Skip to content

Commit 3dfabcb

Browse files
committed
[C++11] Add two range adaptor views to User: operands and
operand_values. The first provides a range view over operand Use objects, and the second provides a range view over the Value*s being used by those operands. The naming is "STL-style" rather than "LLVM-style" because we have historically named iterator methods STL-style, and range methods seem to have far more in common with their iterator counterparts than with "normal" APIs. Feel free to bikeshed on this one if you want, I'm happy to change these around if people feel strongly. I've switched code in SROA and LCG to exercise these mostly to ensure they work correctly -- we don't really have an easy way to unittest this and they're trivial. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202687 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 26abef7 commit 3dfabcb

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

include/llvm/IR/User.h

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef LLVM_IR_USER_H
2020
#define LLVM_IR_USER_H
2121

22+
#include "llvm/ADT/iterator_range.h"
2223
#include "llvm/IR/Value.h"
2324
#include "llvm/Support/ErrorHandling.h"
2425

@@ -112,11 +113,19 @@ class User : public Value {
112113
//
113114
typedef Use* op_iterator;
114115
typedef const Use* const_op_iterator;
116+
typedef iterator_range<op_iterator> op_range;
117+
typedef iterator_range<const_op_iterator> const_op_range;
115118

116119
inline op_iterator op_begin() { return OperandList; }
117120
inline const_op_iterator op_begin() const { return OperandList; }
118121
inline op_iterator op_end() { return OperandList+NumOperands; }
119122
inline const_op_iterator op_end() const { return OperandList+NumOperands; }
123+
inline op_range operands() {
124+
return {op_begin(), op_end()};
125+
}
126+
inline const_op_range operands() const {
127+
return {op_begin(), op_end()};
128+
}
120129

121130
/// Convenience iterator for directly iterating over the Values in the
122131
/// OperandList
@@ -156,6 +165,9 @@ class User : public Value {
156165
inline value_op_iterator value_op_end() {
157166
return value_op_iterator(op_end());
158167
}
168+
inline iterator_range<value_op_iterator> operand_values() {
169+
return {value_op_begin(), value_op_end()};
170+
}
159171

160172
// dropAllReferences() - This function is in charge of "letting go" of all
161173
// objects that this User refers to. This allows one to

lib/Analysis/LazyCallGraph.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ static void findCallees(
4040
continue;
4141
}
4242

43-
for (User::value_op_iterator OI = C->value_op_begin(),
44-
OE = C->value_op_end();
45-
OI != OE; ++OI)
46-
if (Visited.insert(cast<Constant>(*OI)))
47-
Worklist.push_back(cast<Constant>(*OI));
43+
for (Value *Op : C->operand_values())
44+
if (Visited.insert(cast<Constant>(Op)))
45+
Worklist.push_back(cast<Constant>(Op));
4846
}
4947
}
5048

@@ -56,10 +54,8 @@ LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) : G(G), F(F) {
5654
for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
5755
for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
5856
++II)
59-
for (User::value_op_iterator OI = II->value_op_begin(),
60-
OE = II->value_op_end();
61-
OI != OE; ++OI)
62-
if (Constant *C = dyn_cast<Constant>(*OI))
57+
for (Value *Op : II->operand_values())
58+
if (Constant *C = dyn_cast<Constant>(Op))
6359
if (Visited.insert(C))
6460
Worklist.push_back(C);
6561

lib/Transforms/Scalar/SROA.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -3447,9 +3447,8 @@ bool SROA::runOnAlloca(AllocaInst &AI) {
34473447
DE = S.dead_user_end();
34483448
DI != DE; ++DI) {
34493449
// Free up everything used by this instruction.
3450-
for (User::op_iterator DOI = (*DI)->op_begin(), DOE = (*DI)->op_end();
3451-
DOI != DOE; ++DOI)
3452-
clobberUse(*DOI);
3450+
for (Use &DeadOp : (*DI)->operands())
3451+
clobberUse(DeadOp);
34533452

34543453
// Now replace the uses of this instruction.
34553454
(*DI)->replaceAllUsesWith(UndefValue::get((*DI)->getType()));
@@ -3498,10 +3497,10 @@ void SROA::deleteDeadInstructions(SmallPtrSet<AllocaInst*, 4> &DeletedAllocas) {
34983497

34993498
I->replaceAllUsesWith(UndefValue::get(I->getType()));
35003499

3501-
for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
3502-
if (Instruction *U = dyn_cast<Instruction>(*OI)) {
3500+
for (Use &Operand : I->operands())
3501+
if (Instruction *U = dyn_cast<Instruction>(Operand)) {
35033502
// Zero out the operand and see if it becomes trivially dead.
3504-
*OI = 0;
3503+
Operand = 0;
35053504
if (isInstructionTriviallyDead(U))
35063505
DeadInsts.insert(U);
35073506
}

0 commit comments

Comments
 (0)