Skip to content

Commit

Permalink
[InstCombine] Insert instructions before adding them to worklist
Browse files Browse the repository at this point in the history
Summary:
This patch adds instructions to the InstCombine worklist after they are properly inserted. This way we don't get `<badref>`s printed when logging added instructions.
It also adds a check in `Worklist::Add` that ensures that all added instructions have parents.

Simple test case that illustrates the difference when run with `--debug-only=instcombine`:

```
define i32 @test35(i32 %a, i32 %b) {
  %1 = or i32 %a, 1135
  %2 = or i32 %1, %b
  ret i32 %2
}
```

Before this patch:
```
INSTCOMBINE ITERATION #1 on test35
IC: ADDING: 3 instrs to worklist
IC: Visiting:   %1 = or i32 %a, 1135
IC: Visiting:   %2 = or i32 %1, %b
IC: ADD:   %2 = or i32 %a, %b
IC: Old =   %3 = or i32 %1, %b
    New =   <badref> = or i32 %2, 1135
IC: ADD:   <badref> = or i32 %2, 1135
...
```

With this patch:
```
INSTCOMBINE ITERATION #1 on test35
IC: ADDING: 3 instrs to worklist
IC: Visiting:   %1 = or i32 %a, 1135
IC: Visiting:   %2 = or i32 %1, %b
IC: ADD:   %2 = or i32 %a, %b
IC: Old =   %3 = or i32 %1, %b
    New =   <badref> = or i32 %2, 1135
IC: ADD:   %3 = or i32 %2, 1135
...
```

Reviewers: fhahn, davide, spatel, foad, grosser, nikic

Reviewed By: nikic

Subscribers: nikic, lebedev.ri, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D71093
  • Loading branch information
kuhar committed Dec 18, 2019
1 parent 11d5fa6 commit 3d29c41
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace llvm {
/// InstCombineWorklist - This is the worklist management logic for
/// InstCombine.
class InstCombineWorklist {
SmallVector<Instruction*, 256> Worklist;
DenseMap<Instruction*, unsigned> WorklistMap;
SmallVector<Instruction *, 256> Worklist;
DenseMap<Instruction *, unsigned> WorklistMap;

public:
InstCombineWorklist() = default;
Expand All @@ -38,6 +38,9 @@ class InstCombineWorklist {
/// Add - Add the specified instruction to the worklist if it isn't already
/// in it.
void Add(Instruction *I) {
assert(I);
assert(I->getParent() && "Instruction not inserted yet?");

if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) {
LLVM_DEBUG(dbgs() << "IC: ADD: " << *I << '\n');
Worklist.push_back(I);
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,17 @@ Instruction *InstCombiner::visitZExt(ZExtInst &CI) {
// zext (or icmp, icmp) -> or (zext icmp), (zext icmp)
Value *LCast = Builder.CreateZExt(LHS, CI.getType(), LHS->getName());
Value *RCast = Builder.CreateZExt(RHS, CI.getType(), RHS->getName());
BinaryOperator *Or = BinaryOperator::Create(Instruction::Or, LCast, RCast);
Value *Or = Builder.CreateOr(LCast, RCast, CI.getName());
if (auto *OrInst = dyn_cast<Instruction>(Or))
Builder.SetInsertPoint(OrInst);

// Perform the elimination.
if (auto *LZExt = dyn_cast<ZExtInst>(LCast))
transformZExtICmp(LHS, *LZExt);
if (auto *RZExt = dyn_cast<ZExtInst>(RCast))
transformZExtICmp(RHS, *RZExt);

return Or;
return replaceInstUsesWith(CI, Or);
}
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3361,10 +3361,6 @@ bool InstCombiner::run() {
// Move the name to the new instruction first.
Result->takeName(I);

// Push the new instruction and any users onto the worklist.
Worklist.AddUsersToWorkList(*Result);
Worklist.Add(Result);

// Insert the new instruction into the basic block...
BasicBlock *InstParent = I->getParent();
BasicBlock::iterator InsertPos = I->getIterator();
Expand All @@ -3376,6 +3372,10 @@ bool InstCombiner::run() {

InstParent->getInstList().insert(InsertPos, Result);

// Push the new instruction and any users onto the worklist.
Worklist.AddUsersToWorkList(*Result);
Worklist.Add(Result);

eraseInstFromFunction(*I);
} else {
LLVM_DEBUG(dbgs() << "IC: Mod = " << OrigI << '\n'
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/zext-or-icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ define i8 @zext_or_icmp_icmp(i8 %a, i8 %b) {
; CHECK-NEXT: %toBool2 = icmp eq i8 %b, 0
; CHECK-NEXT: %toBool22 = zext i1 %toBool2 to i8
; CHECK-NEXT: %1 = xor i8 %mask, 1
; CHECK-NEXT: %zext = or i8 %1, %toBool22
; CHECK-NEXT: %zext3 = or i8 %1, %toBool22
; CHECK-NEXT: ret i8 %zext
}

Expand Down

0 comments on commit 3d29c41

Please sign in to comment.