llvm.org GIT mirror llvm / b00c582
Commit more code over to new cast style git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@697 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 18 years ago
42 changed file(s) with 351 addition(s) and 235 deletion(s). Raw diff Collapse all Expand all
0 * grep '[A-Za-z][A-Za-z]*\*)' `./getsrcs.sh ` | & less
1
2
3 * Need to implement getelementptr, load, and store for indirection through
14 arrays and multidim arrays
25 * Indirect calls should use the icall instruction
261261
262262

                  
                
263263 inline Value *getOperand(unsigned i) {
264 assert(i < Operands.size() && "getOperand() out of range!");
264 assert(i < Operands.size() && "getOperand() out of range!");
265265 return Operands[i];
266266 }
267267
269269 Here are some examples:
270270
271271

                  
                
272 assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
272 assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
273273
274274 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
275275
276 assert(idx < getNumSuccessors() && "Successor # out of range!");
276 assert(idx < getNumSuccessors() && "Successor # out of range!");
277277
278278 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
279279
280 assert(Succ->front()->isPHINode() && "Only works on PHId BBs!");
280 assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!");
281281

282282
283283 You get the idea...

645645
Chris Lattner
646646
647647
648 Last modified: Mon Oct 1 08:17:21 CDT 2001
648 Last modified: Mon Oct 1 15:33:40 CDT 2001
649649
650650
651651
9191 inline ostream &operator<<(ostream &o, const Value *I) {
9292 switch (I->getValueType()) {
9393 case Value::TypeVal: return o << cast(I);
94 case Value::ConstantVal: WriteToAssembly((const ConstPoolVal*)I,o);break;
94 case Value::ConstantVal: WriteToAssembly(cast(I) , o); break;
9595 case Value::MethodArgumentVal: return o << I->getType() << " "<< I->getName();
96 case Value::InstructionVal:WriteToAssembly((const Instruction *)I, o);break;
97 case Value::BasicBlockVal: WriteToAssembly((const BasicBlock *)I, o);break;
98 case Value::MethodVal: WriteToAssembly((const Method *)I, o);break;
99 case Value::GlobalVal: WriteToAssembly((const GlobalVariable*)I,o);break;
100 case Value::ModuleVal: WriteToAssembly((const Module *)I,o); break;
96 case Value::InstructionVal:WriteToAssembly(cast(I) , o); break;
97 case Value::BasicBlockVal: WriteToAssembly(cast(I) , o); break;
98 case Value::MethodVal: WriteToAssembly(cast(I) , o); break;
99 case Value::GlobalVal: WriteToAssembly(cast(I), o); break;
100 case Value::ModuleVal: WriteToAssembly(cast(I) , o); break;
101101 default: return o << "getValueType() << ">";
102102 }
103103 return o;
109109 InstListType &getInstList() { return InstList; }
110110
111111 // Methods for support type inquiry through isa, cast, and dyn_cast:
112 static inline bool isa(const BasicBlock *BB) { return true; }
113 static inline bool isa(const Value *V) {
112 static inline bool classof(const BasicBlock *BB) { return true; }
113 static inline bool classof(const Value *V) {
114114 return V->getValueType() == Value::BasicBlockVal;
115115 }
116116
167167 inline void advancePastConstPool() {
168168 // TODO: This is bad
169169 // Loop to ignore constant pool references
170 while (It != BB->use_end() &&
171 (((*It)->getValueType() != Value::InstructionVal) ||
172 !(((Instruction*)(*It))->isTerminator())))
170 while (It != BB->use_end() && !isa(*It))
173171 ++It;
174172 }
175173
170170
171171 Instruction *getInstruction() const {
172172 assert(treeNodeType == NTInstructionNode);
173 return (Instruction*)val;
173 return cast(val);
174174 }
175175 protected:
176176 virtual void dumpNode(int indent) const;
233233 //
234234 //------------------------------------------------------------------------
235235
236 class InstrForest : private hash_map*, InstructionNode*> {
236 class InstrForest : private hash_map *, InstructionNode*> {
237237 private:
238238 hash_set treeRoots;
239239
3333 static ConstPoolVal *getNullConstant(const Type *Ty);
3434
3535 // Methods for support type inquiry through isa, cast, and dyn_cast:
36 static inline bool isa(const ConstPoolVal *) { return true; }
37 static inline bool isa(const Value *V) {
36 static inline bool classof(const ConstPoolVal *) { return true; }
37 static inline bool classof(const Value *V) {
3838 return V->getValueType() == Value::ConstantVal;
3939 }
4040 };
6767 inline bool getValue() const { return Val; }
6868
6969 // Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool isa(const ConstPoolBool *) { return true; }
71 static bool isa(const ConstPoolVal *CPV) {
70 static inline bool classof(const ConstPoolBool *) { return true; }
71 static bool classof(const ConstPoolVal *CPV) {
7272 return (CPV == True) | (CPV == False);
7373 }
74 static inline bool isa(const Value *V) {
75 return ::isa(V) && isa(cast(V));
74 static inline bool classof(const Value *V) {
75 return isa(V) && classof(cast(V));
7676 }
7777 };
7878
107107 static ConstPoolInt *get(const Type *Ty, unsigned char V);
108108
109109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110 static inline bool isa(const ConstPoolInt *) { return true; }
111 static bool isa(const ConstPoolVal *CPV); // defined in CPV.cpp
112 static inline bool isa(const Value *V) {
113 return ::isa(V) && isa(cast(V));
110 static inline bool classof(const ConstPoolInt *) { return true; }
111 static bool classof(const ConstPoolVal *CPV); // defined in CPV.cpp
112 static inline bool classof(const Value *V) {
113 return isa(V) && classof(cast(V));
114114 }
115115 };
116116
7979 void refineAbstractTypeTo(const Type *NewType);
8080
8181 // Methods for support type inquiry through isa, cast, and dyn_cast:
82 static inline bool isa(const DerivedType *T) { return true; }
83 static inline bool isa(const Type *T) {
82 static inline bool classof(const DerivedType *T) { return true; }
83 static inline bool classof(const Type *T) {
8484 return T->isDerivedType();
8585 }
86 static inline bool isa(const Value *V) {
87 return ::isa(V) && isa(cast(V));
86 static inline bool classof(const Value *V) {
87 return isa(V) && classof(cast(V));
8888 }
8989 };
9090
132132
133133
134134 // Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool isa(const MethodType *T) { return true; }
136 static inline bool isa(const Type *T) {
135 static inline bool classof(const MethodType *T) { return true; }
136 static inline bool classof(const Type *T) {
137137 return T->getPrimitiveID() == MethodTyID;
138138 }
139 static inline bool isa(const Value *V) {
140 return ::isa(V) && isa(cast(V));
139 static inline bool classof(const Value *V) {
140 return isa(V) && classof(cast(V));
141141 }
142142 };
143143
180180 static ArrayType *get(const Type *ElementType, int NumElements = -1);
181181
182182 // Methods for support type inquiry through isa, cast, and dyn_cast:
183 static inline bool isa(const ArrayType *T) { return true; }
184 static inline bool isa(const Type *T) {
183 static inline bool classof(const ArrayType *T) { return true; }
184 static inline bool classof(const Type *T) {
185185 return T->getPrimitiveID() == ArrayTyID;
186186 }
187 static inline bool isa(const Value *V) {
188 return ::isa(V) && isa(cast(V));
187 static inline bool classof(const Value *V) {
188 return isa(V) && classof(cast(V));
189189 }
190190 };
191191
225225 static StructType *get(const vector &Params);
226226
227227 // Methods for support type inquiry through isa, cast, and dyn_cast:
228 static inline bool isa(const StructType *T) { return true; }
229 static inline bool isa(const Type *T) {
228 static inline bool classof(const StructType *T) { return true; }
229 static inline bool classof(const Type *T) {
230230 return T->getPrimitiveID() == StructTyID;
231231 }
232 static inline bool isa(const Value *V) {
233 return ::isa(V) && isa(cast(V));
232 static inline bool classof(const Value *V) {
233 return isa(V) && classof(cast(V));
234234 }
235235 };
236236
268268 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
269269
270270 // Methods for support type inquiry through isa, cast, and dyn_cast:
271 static inline bool isa(const PointerType *T) { return true; }
272 static inline bool isa(const Type *T) {
271 static inline bool classof(const PointerType *T) { return true; }
272 static inline bool classof(const Type *T) {
273273 return T->getPrimitiveID() == PointerTyID;
274274 }
275 static inline bool isa(const Value *V) {
276 return ::isa(V) && isa(cast(V));
275 static inline bool classof(const Value *V) {
276 return isa(V) && classof(cast(V));
277277 }
278278 };
279279
298298 }
299299
300300 // Methods for support type inquiry through isa, cast, and dyn_cast:
301 static inline bool isa(const OpaqueType *T) { return true; }
302 static inline bool isa(const Type *T) {
301 static inline bool classof(const OpaqueType *T) { return true; }
302 static inline bool classof(const Type *T) {
303303 return T->getPrimitiveID() == OpaqueTyID;
304304 }
305 static inline bool isa(const Value *V) {
306 return ::isa(V) && isa(cast(V));
305 static inline bool classof(const Value *V) {
306 return isa(V) && classof(cast(V));
307307 }
308308 };
309309
9292
9393
9494 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool isa(const Method *T) { return true; }
96 static inline bool isa(const Value *V) {
95 static inline bool classof(const Method *T) { return true; }
96 static inline bool classof(const Value *V) {
9797 return V->getValueType() == Value::MethodVal;
9898 }
9999
6060 inline bool isConstant() const { return Constant; }
6161
6262 // Methods for support type inquiry through isa, cast, and dyn_cast:
63 static inline bool isa(const GlobalVariable *) { return true; }
64 static inline bool isa(const Value *V) {
63 static inline bool classof(const GlobalVariable *) { return true; }
64 static inline bool classof(const Value *V) {
6565 return V->getValueType() == Value::GlobalVal;
6666 }
6767 };
3939 inline BasicBlock *getSuccessor(unsigned idx) {
4040 return (BasicBlock*)((const TerminatorInst *)this)->getSuccessor(idx);
4141 }
42
43 // Methods for support type inquiry through isa, cast, and dyn_cast:
44 static inline bool classof(const TerminatorInst *) { return true; }
45 static inline bool classof(const Instruction *I) {
46 return I->getOpcode() >= FirstTermOp && I->getOpcode() < NumTermOps;
47 }
48 static inline bool classof(const Value *V) {
49 return isa(V) && classof(cast(V));
50 }
4251 };
4352
4453
6978 }
7079
7180 virtual const char *getOpcodeName() const = 0;
81
82 // Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const UnaryOperator *) { return true; }
84 static inline bool classof(const Instruction *I) {
85 return I->getOpcode() >= FirstUnaryOp && I->getOpcode() < NumUnaryOps;
86 }
87 static inline bool classof(const Value *V) {
88 return isa(V) && classof(cast(V));
89 }
7290 };
7391
7492
110128 void swapOperands() {
111129 swap(Operands[0], Operands[1]);
112130 }
131
132 // Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const BinaryOperator *) { return true; }
134 static inline bool classof(const Instruction *I) {
135 return I->getOpcode() >= FirstBinaryOp && I->getOpcode() < NumBinaryOps;
136 }
137 static inline bool classof(const Value *V) {
138 return isa(V) && classof(cast(V));
139 }
113140 };
114141
115142 #endif
6565 unsigned getInstType() const { return iType; }
6666
6767 inline bool isTerminator() const { // Instance of TerminatorInst?
68 return iType >= FirstTermOp && iType < NumTermOps;
68 return iType >= FirstTermOp && iType < NumTermOps;
6969 }
7070 inline bool isDefinition() const { return !isTerminator(); }
7171 inline bool isUnaryOp() const {
7474 inline bool isBinaryOp() const {
7575 return iType >= FirstBinaryOp && iType < NumBinaryOps;
7676 }
77
78 // isPHINode() - This is used frequently enough to allow it to exist
79 inline bool isPHINode() const { return iType == PHINode; }
8077
8178 // dropAllReferences() - This function is in charge of "letting go" of all
8279 // objects that this Instruction refers to. This first lets go of all
8784
8885
8986 // Methods for support type inquiry through isa, cast, and dyn_cast:
90 static inline bool isa(const Instruction *I) { return true; }
91 static inline bool isa(const Value *V) {
87 static inline bool classof(const Instruction *I) { return true; }
88 static inline bool classof(const Value *V) {
9289 return V->getValueType() == Value::InstructionVal;
9390 }
9491
9393 inline Method *back() { return MethodList.back(); }
9494
9595 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool isa(const Module *T) { return true; }
97 static inline bool isa(const Value *V) {
96 static inline bool classof(const Module *T) { return true; }
97 static inline bool classof(const Value *V) {
9898 return V->getValueType() == Value::ModuleVal;
9999 }
100100
188188 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; }
189189
190190 // Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool isa(const Type *T) { return true; }
192 static inline bool isa(const Value *V) {
191 static inline bool classof(const Type *T) { return true; }
192 static inline bool classof(const Value *V) {
193193 return V->getValueType() == Value::TypeVal;
194194 }
195195
196 // Methods for determining the subtype of this Type. The cast*() methods are
197 // equilivent to using dynamic_cast<>... if the cast is successful, this is
198 // returned, otherwise you get a null pointer, allowing expressions like this:
199 //
200 // if (MethodType *MTy = Ty->dyncastMethodType()) { ... }
201 //
202 // This section also defines a family of isArrayType(), isLabelType(),
203 // etc functions...
204 //
205 // The family of functions Ty->cast() is used in the same way as the
206 // Ty->dyncast() instructions, but they assert the expected type instead
207 // of checking it at runtime.
196 // Methods for determining the subtype of this Type. This section defines a
197 // family of isArrayType(), isLabelType(), etc functions...
208198 //
209199 #define HANDLE_PRIM_TYPE(NAME, SIZE) \
210200 inline bool is##NAME##Type() const { return ID == NAME##TyID; }
211201 #define HANDLE_DERV_TYPE(NAME, CLASS) \
212 inline bool is##NAME##Type() const { return ID == NAME##TyID; } \
213 inline const CLASS *dyncast##NAME##Type() const { /*const version */ \
214 return is##NAME##Type() ? (const CLASS*)this : 0; \
215 } \
216 inline CLASS *dyncast##NAME##Type() { /* nonconst version */ \
217 return is##NAME##Type() ? (CLASS*)this : 0; \
218 } \
219 inline const CLASS *cast##NAME##Type() const { /*const version */ \
220 assert(is##NAME##Type() && "Expected TypeTy: " #NAME); \
221 return (const CLASS*)this; \
222 } \
223 inline CLASS *cast##NAME##Type() { /* nonconst version */ \
224 assert(is##NAME##Type() && "Expected TypeTy: " #NAME); \
225 return (CLASS*)this; \
226 }
202 inline bool is##NAME##Type() const { return ID == NAME##TyID; }
203
227204 #include "llvm/Type.def"
228205
229206 private:
184184 // if (isa(myVal)) { ... }
185185 //
186186 template
187 inline bool isa(Y Val) { return X::isa(Val); }
187 inline bool isa(Y Val) { return X::classof(Val); }
188188
189189
190190 // cast - Return the argument parameter cast to the specified type. This
2626
2727 if (ArraySize) {
2828 // Make sure they didn't try to specify a size for !(unsized array) type
29 assert((getType()->getValueType()->isArrayType() &&
30 ((const ArrayType*)getType()->getValueType())->isUnsized()) &&
31 "Trying to allocate something other than unsized array, with size!");
29 assert(getType()->getValueType()->isArrayType() &&
30 cast(getType()->getValueType())->isUnsized() &&
31 "Trying to allocate something other than unsized array, with size!");
3232
3333 Operands.reserve(1);
3434 Operands.push_back(Use(ArraySize, this));
3535 } else {
3636 // Make sure that the pointer is not to an unsized array!
3737 assert(!getType()->getValueType()->isArrayType() ||
38 ((const ArrayType*)getType()->getValueType())->isSized() &&
38 cast(getType()->getValueType())->isSized() &&
3939 "Trying to allocate unsized array without size!");
4040 }
4141 }
6363 }
6464
6565 virtual const char *getOpcodeName() const { return "malloc"; }
66
67 // Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const MallocInst *) { return true; }
69 static inline bool classof(const Instruction *I) {
70 return (I->getOpcode() == Instruction::Malloc);
71 }
72 static inline bool classof(const Value *V) {
73 return isa(V) && classof(cast(V));
74 }
6675 };
6776
6877
8089 }
8190
8291 virtual const char *getOpcodeName() const { return "alloca"; }
92
93 // Methods for support type inquiry through isa, cast, and dyn_cast:
94 static inline bool classof(const AllocaInst *) { return true; }
95 static inline bool classof(const Instruction *I) {
96 return (I->getOpcode() == Instruction::Alloca);
97 }
98 static inline bool classof(const Value *V) {
99 return isa(V) && classof(cast(V));
100 }
83101 };
84102
85103
101119 virtual const char *getOpcodeName() const { return "free"; }
102120
103121 virtual bool hasSideEffects() const { return true; }
122
123 // Methods for support type inquiry through isa, cast, and dyn_cast:
124 static inline bool classof(const FreeInst *) { return true; }
125 static inline bool classof(const Instruction *I) {
126 return (I->getOpcode() == Instruction::Free);
127 }
128 static inline bool classof(const Value *V) {
129 return isa(V) && classof(cast(V));
130 }
104131 };
105132
106133
159186 virtual const char* getOpcodeName() const { return "load"; }
160187 virtual Value* getPtrOperand() { return this->getOperand(0); }
161188 virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
189
190 // Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool classof(const LoadInst *) { return true; }
192 static inline bool classof(const Instruction *I) {
193 return (I->getOpcode() == Instruction::Load);
194 }
195 static inline bool classof(const Value *V) {
196 return isa(V) && classof(cast(V));
197 }
162198 };
163199
164200
181217 virtual bool hasSideEffects() const { return true; }
182218 virtual Value* getPtrOperand() { return this->getOperand(1); }
183219 virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 2)? 2 : -1;}
220
221 // Methods for support type inquiry through isa, cast, and dyn_cast:
222 static inline bool classof(const StoreInst *) { return true; }
223 static inline bool classof(const Instruction *I) {
224 return (I->getOpcode() == Instruction::Store);
225 }
226 static inline bool classof(const Value *V) {
227 return isa(V) && classof(cast(V));
228 }
184229 };
185230
186231
205250
206251 inline bool isArraySelector() const { return !isStructSelector(); }
207252 bool isStructSelector() const;
253
254
255 // Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const GetElementPtrInst *) { return true; }
257 static inline bool classof(const Instruction *I) {
258 return (I->getOpcode() == Instruction::GetElementPtr);
259 }
260 static inline bool classof(const Value *V) {
261 return isa(V) && classof(cast(V));
262 }
208263 };
209264
210265 #endif // LLVM_IMEMORY_H
5454 // removeIncomingValue - Remove an incoming value. This is useful if a
5555 // predecessor basic block is deleted. The value removed is returned.
5656 Value *removeIncomingValue(const BasicBlock *BB);
57
58
59 // Methods for support type inquiry through isa, cast, and dyn_cast:
60 static inline bool classof(const PHINode *) { return true; }
61 static inline bool classof(const Instruction *I) {
62 return I->getOpcode() == Instruction::PHINode;
63 }
64 static inline bool classof(const Value *V) {
65 return isa(V) && classof(cast(V));
66 }
5767 };
5868
5969
7888
7989 virtual Instruction *clone() const { return new CastInst(*this); }
8090 virtual const char *getOpcodeName() const { return "cast"; }
91
92 // Methods for support type inquiry through isa, cast, and dyn_cast:
93 static inline bool classof(const CastInst *) { return true; }
94 static inline bool classof(const Instruction *I) {
95 return I->getOpcode() == Cast;
96 }
97 static inline bool classof(const Value *V) {
98 return isa(V) && classof(cast(V));
99 }
81100 };
82101
83102
104123 inline Method *getParent() { return Parent; }
105124
106125 // Methods for support type inquiry through isa, cast, and dyn_cast:
107 static inline bool isa(const MethodArgument *) { return true; }
108 static inline bool isa(const Value *V) {
126 static inline bool classof(const MethodArgument *) { return true; }
127 static inline bool classof(const Value *V) {
109128 return V->getValueType() == MethodArgumentVal;
110129 }
111130 };
131150 }
132151 Method *getCalledMethod() {
133152 return cast(Operands[0]);
153 }
154
155 // Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const CallInst *) { return true; }
157 static inline bool classof(const Instruction *I) {
158 return I->getOpcode() == Instruction::Call;
159 }
160 static inline bool classof(const Value *V) {
161 return isa(V) && classof(cast(V));
134162 }
135163 };
136164
160188 virtual const char *getOpcodeName() const {
161189 return getOpcode() == Shl ? "shl" : "shr";
162190 }
191
192 // Methods for support type inquiry through isa, cast, and dyn_cast:
193 static inline bool classof(const ShiftInst *) { return true; }
194 static inline bool classof(const Instruction *I) {
195 return (I->getOpcode() == Instruction::Shr) |
196 (I->getOpcode() == Instruction::Shl);
197 }
198 static inline bool classof(const Value *V) {
199 return isa(V) && classof(cast(V));
200 }
163201 };
164202
165203 #endif
5555 //
5656 virtual const BasicBlock *getSuccessor(unsigned idx) const { return 0; }
5757 virtual unsigned getNumSuccessors() const { return 0; }
58
59 // Methods for support type inquiry through isa, cast, and dyn_cast:
60 static inline bool classof(const ReturnInst *) { return true; }
61 static inline bool classof(const Instruction *I) {
62 return (I->getOpcode() == Instruction::Ret);
63 }
64 static inline bool classof(const Value *V) {
65 return isa(V) && classof(cast(V));
66 }
5867 };
5968
6069
104113 }
105114
106115 virtual unsigned getNumSuccessors() const { return 1+!isUnconditional(); }
116
117 // Methods for support type inquiry through isa, cast, and dyn_cast:
118 static inline bool classof(const BranchInst *) { return true; }
119 static inline bool classof(const Instruction *I) {
120 return (I->getOpcode() == Instruction::Br);
121 }
122 static inline bool classof(const Value *V) {
123 return isa(V) && classof(cast(V));
124 }
107125 };
108126
109127
160178 return cast(Operands[idx*2]);
161179 }
162180 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
181
182 // Methods for support type inquiry through isa, cast, and dyn_cast:
183 static inline bool classof(const SwitchInst *) { return true; }
184 static inline bool classof(const Instruction *I) {
185 return (I->getOpcode() == Instruction::Switch);
186 }
187 static inline bool classof(const Value *V) {
188 return isa(V) && classof(cast(V));
189 }
163190 };
164191
165192 #endif
9696 ConstPoolVal *Result = *Arg1 + *Arg2;
9797 assert(Result && Result->getType() == Arg1->getType() &&
9898 "Couldn't perform addition!");
99 ConstPoolInt *ResultI = (ConstPoolInt*)Result;
99 ConstPoolInt *ResultI = cast(Result);
100100
101101 // Check to see if the result is one of the special cases that we want to
102102 // recognize...
146146 ConstPoolVal *Result = *Arg1 * *Arg2;
147147 assert(Result && Result->getType() == Arg1->getType() &&
148148 "Couldn't perform mult!");
149 ConstPoolInt *ResultI = (ConstPoolInt*)Result;
149 ConstPoolInt *ResultI = cast(Result);
150150
151151 // Check to see if the result is one of the special cases that we want to
152152 // recognize...
202202 const Type *ETy = E.getExprType(Ty);
203203 ConstPoolInt *Zero = getUnsignedConstant(0, ETy);
204204 ConstPoolInt *One = getUnsignedConstant(1, ETy);
205 ConstPoolInt *NegOne = (ConstPoolInt*)(*Zero - *One);
205 ConstPoolInt *NegOne = cast(*Zero - *One);
206206 if (NegOne == 0) return V; // Couldn't subtract values...
207207
208208 return ExprType(DefOne (E.Scale , Ty) * NegOne, E.Var,
229229 case Value::ConstantVal: // Constant value, just return constant
230230 ConstPoolVal *CPV = cast(Expr);
231231 if (CPV->getType()->isIntegral()) { // It's an integral constant!
232 ConstPoolInt *CPI = (ConstPoolInt*)Expr;
232 ConstPoolInt *CPI = cast(Expr);
233233 return ExprType(CPI->equalsInt(0) ? 0 : CPI);
234234 }
235235 return Expr;
296296 const ConstPoolVal *CPV =ConstRules::get(*Offs)->castTo(Offs, DestTy);
297297 if (!CPV) return I;
298298 assert(CPV->getType()->isIntegral() && "Must have an integral type!");
299 return (ConstPoolInt*)CPV;
299 return cast(CPV);
300300 } // end case Instruction::Cast
301301 // TODO: Handle SUB, SHR?
302302
3737
3838 for (Method::inst_iterator II = M->inst_begin(), IE = M->inst_end();
3939 II != IE; ++II) {
40 if (II->getOpcode() == Instruction::Call) {
41 CallInst *CI = (CallInst*)*II;
40 if (CallInst *CI = dyn_cast(*II))
4241 Node->addCalledMethod(getNodeFor(CI->getCalledMethod()));
43 }
4442 }
4543 }
4644
4545 break;
4646
4747 case Type::StructTyID: {
48 const StructType *ST = (const StructType*)T;
48 const StructType *ST = cast(T);
4949 const StructType::ElementTypes &Elements = ST->getElementTypes();
5050 for (StructType::ElementTypes::const_iterator I = Elements.begin();
5151 I != Elements.end(); ++I)
396396 // There is only one case where this is allowed: when we are refining an
397397 // opaque type. In this case, Existing will be an opaque type.
398398 if (const Type *Ty = cast(Existing))
399 if (Ty->isOpaqueType()) {
399 if (OpaqueType *OpTy = dyn_cast(Ty)) {
400400 // We ARE replacing an opaque type!
401
402 cast(Ty)->refineAbstractTypeTo(cast(V));
401 OpTy->refineAbstractTypeTo(cast(V));
403402 return;
404403 }
405404
12311230 while ($2->begin() != $2->end()) {
12321231 if ($2->front().first->getType() != Ty)
12331232 ThrowException("All elements of a PHI node must be of the same type!");
1234 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
1233 cast($$)->addIncoming($2->front().first, $2->front().second);
12351234 $2->pop_front();
12361235 }
12371236 delete $2; // Free the list...
12901289 delete $2;
12911290 }
12921291 | MALLOC Types ',' UINT ValueRef {
1293 if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1292 if (!(*$2)->isArrayType() || cast($2->get())->isSized())
12941293 ThrowException("Trying to allocate " + (*$2)->getName() +
12951294 " as unsized array!");
12961295 const Type *Ty = PointerType::get(*$2);
13021301 delete $2;
13031302 }
13041303 | ALLOCA Types ',' UINT ValueRef {
1305 if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1304 if (!(*$2)->isArrayType() || cast($2->get())->isSized())
13061305 ThrowException("Trying to allocate " + (*$2)->getName() +
13071306 " as unsized array!");
13081307 const Type *Ty = PointerType::get(*$2);
228228 abort();
229229
230230 case Type::ArrayTyID: {
231 const ArrayType *AT = (const ArrayType*)Ty;
231 const ArrayType *AT = cast(Ty);
232232 unsigned NumElements;
233233 if (AT->isSized()) // Sized array, # elements stored in type!
234234 NumElements = (unsigned)AT->getNumElements();
248248 }
249249
250250 case Type::StructTyID: {
251 const StructType *ST = Ty->castStructType();
251 const StructType *ST = cast(Ty);
252252 const StructType::ElementTypes &ET = ST->getElementTypes();
253253
254254 vector Elements;
266266 }
267267
268268 case Type::PointerTyID: {
269 const PointerType *PT = Ty->castPointerType();
269 const PointerType *PT = cast(Ty);
270270 unsigned SubClass;
271271 if (read_vbr(Buf, EndBuf, SubClass)) return failure(true);
272272 if (SubClass != 0) return failure(true);
121121 delete PN;
122122 return failure(true);
123123 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
124 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
124 cast(getValue(Type::LabelTy,Raw.Arg2)));
125125 break;
126126 default:
127127 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
128 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
128 cast(getValue(Type::LabelTy, Raw.Arg2)));
129129 if (Raw.VarArgs->size() & 1) {
130130 cerr << "PHI Node with ODD number of arguments!\n";
131131 delete PN;
134134 vector &args = *Raw.VarArgs;
135135 for (unsigned i = 0; i < args.size(); i+=2)
136136 PN->addIncoming(getValue(Raw.Ty, args[i]),
137 (BasicBlock*)getValue(Type::LabelTy, args[i+1]));
137 cast(getValue(Type::LabelTy, args[i+1])));
138138 }
139139 delete Raw.VarArgs;
140140 break;
159159
160160 case Instruction::Br:
161161 if (Raw.NumOperands == 1) {
162 Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1));
162 Res = new BranchInst(cast(getValue(Type::LabelTy, Raw.Arg1)));
163163 return false;
164164 } else if (Raw.NumOperands == 3) {
165 Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1),
166 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2),
167 getValue(Type::BoolTy , Raw.Arg3));
165 Res = new BranchInst(cast(getValue(Type::LabelTy, Raw.Arg1)),
166 cast(getValue(Type::LabelTy, Raw.Arg2)),
167 getValue(Type::BoolTy , Raw.Arg3));
168168 return false;
169169 }
170170 break;
172172 case Instruction::Switch: {
173173 SwitchInst *I =
174174 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
175 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
175 cast(getValue(Type::LabelTy, Raw.Arg2)));
176176 Res = I;
177177 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
178178
184184
185185 vector &args = *Raw.VarArgs;
186186 for (unsigned i = 0; i < args.size(); i += 2)
187 I->dest_push_back((ConstPoolVal*)getValue(Raw.Ty, args[i]),
188 (BasicBlock*)getValue(Type::LabelTy, args[i+1]));
187 I->dest_push_back(cast(getValue(Raw.Ty, args[i])),
188 cast(getValue(Type::LabelTy, args[i+1])));
189189
190190 delete Raw.VarArgs;
191191 return false;
192192 }
193193
194194 case Instruction::Call: {
195 Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1);
195 Method *M = cast(getValue(Raw.Ty, Raw.Arg1));
196196 if (M == 0) return failure(true);
197197
198198 vector Params;
337337 unsigned InitSlot;
338338 if (read_vbr(Buf, End, InitSlot)) return failure(true);
339339
340 Value *V = getValue(Ty->castPointerType()->getValueType(),
340 Value *V = getValue(cast(Ty)->getValueType(),
341341 InitSlot, false);
342342 if (V == 0) return failure(true);
343343 Initializer = cast(V);
381381 // Keep track of this information in a linked list that is emptied as
382382 // methods are loaded...
383383 //
384 MethodSignatureList.push_back(make_pair((const MethodType*)Ty, SlotNo));
384 MethodSignatureList.push_back(make_pair(cast(Ty),SlotNo));
385385 if (read_vbr(Buf, End, MethSignature)) return failure(true);
386386 BCR_TRACE(2, "Method of type: " << Ty << endl);
387387 }
2222
2323 switch (T->getPrimitiveID()) { // Handle derived types now.
2424 case Type::MethodTyID: {
25 const MethodType *MT = (const MethodType*)T;
25 const MethodType *MT = cast(T);
2626 int Slot = Table.getValSlot(MT->getReturnType());
2727 assert(Slot != -1 && "Type used but not available!!");
2828 output_vbr((unsigned)Slot, Out);
4545 }
4646
4747 case Type::ArrayTyID: {
48 const ArrayType *AT = (const ArrayType*)T;
48 const ArrayType *AT = cast(T);
4949 int Slot = Table.getValSlot(AT->getElementType());
5050 assert(Slot != -1 && "Type used but not available!!");
5151 output_vbr((unsigned)Slot, Out);
5656 }
5757
5858 case Type::StructTyID: {
59 const StructType *ST = (const StructType*)T;
59 const StructType *ST = cast(T);
6060
6161 // Output all of the element types...
6262 StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
7272 }
7373
7474 case Type::PointerTyID: {
75 const PointerType *PT = (const PointerType*)T;
75 const PointerType *PT = cast(T);
7676 int Slot = Table.getValSlot(PT->getValueType());
7777 assert(Slot != -1 && "Type used but not available!!");
7878 output_vbr((unsigned)Slot, Out);
9090 bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
9191 switch (CPV->getType()->getPrimitiveID()) {
9292 case Type::BoolTyID: // Boolean Types
93 if (((const ConstPoolBool*)CPV)->getValue())
93 if (cast(CPV)->getValue())
9494 output_vbr((unsigned)1, Out);
9595 else
9696 output_vbr((unsigned)0, Out);
2020 #include "llvm/Target/MachineInstrInfo.h"
2121 #include "llvm/Target/MachineRegInfo.h"
2222 #include "llvm/Support/StringExtras.h"
23 #include "llvm/iOther.h"
2324 #include
2425
2526
539540 // Phi instructions are the only ones that produce a value but don't get
540541 // any non-dummy machine instructions. Return here as an optimization.
541542 //
542 if (defVMInstr->isPHINode())
543 if (isa(defVMInstr))
543544 return;
544545
545546 // Now add the graph edge for the appropriate machine instruction(s).
641642 SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
642643 const TargetMachine& target)
643644 {
644 if (instr->isPHINode())
645 if (isa(instr))
645646 return;
646647
647648 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
2525 #include "llvm/Method.h"
2626 #include "llvm/iTerminators.h"
2727 #include "llvm/iMemory.h"
28 #include "llvm/iOther.h"
2829 #include "llvm/ConstPoolVals.h"
2930 #include "llvm/BasicBlock.h"
3031 #include "llvm/CodeGen/MachineInstr.h"
5657
5758 // Distinguish special cases of some instructions such as Ret and Br
5859 //
59 if (opLabel == Instruction::Ret && ((ReturnInst*)I)->getReturnValue())
60 if (opLabel == Instruction::Ret && cast(I)->getReturnValue())
6061 {
6162 opLabel = RetValueOp; // ret(value) operation
6263 }
63 else if (opLabel == Instruction::Br && ! ((BranchInst*)I)->isUnconditional())
64 else if (opLabel ==Instruction::Br && !cast(I)->isUnconditional())
6465 {
6566 opLabel = BrCondOp; // br(cond) operation
6667 }
301302 InstrTreeNode* opTreeNode;
302303 if (isa(operand) && operand->use_size() == 1 &&
303304 cast(operand)->getParent() == instr->getParent() &&
304 ! instr->isPHINode() &&
305 !isa(instr) &&
305306 instr->getOpcode() != Instruction::Call)
306307 {
307308 // Recursively create a treeNode for it.
350350 unsigned NumElements = 1;
351351
352352 if (I->getNumOperands()) { // Allocating a unsized array type?
353 assert(Ty->isArrayType() && Ty->castArrayType()->isUnsized() &&
353 assert(isa(Ty) && cast(Ty)->isUnsized() &&
354354 "Allocation inst with size operand for !unsized array type???");
355 Ty = ((const ArrayType*)Ty)->getElementType(); // Get the actual type...
355 Ty = cast(Ty)->getElementType(); // Get the actual type...
356356
357357 // Get the number of elements being allocated by the array...
358358 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
664664 // Memory Instructions
665665 case Instruction::Alloca:
666666 case Instruction::Malloc: executeAllocInst ((AllocationInst*)I, SF); break;
667 case Instruction::Free: executeFreeInst ((FreeInst*) I, SF); break;
668 case Instruction::Load: executeLoadInst ((LoadInst*) I, SF); break;
669 case Instruction::Store: executeStoreInst ((StoreInst*) I, SF); break;
667 case Instruction::Free: executeFreeInst (cast (I), SF); break;
668 case Instruction::Load: executeLoadInst (cast (I), SF); break;
669 case Instruction::Store: executeStoreInst (cast(I), SF); break;
670670
671671 // Miscellaneous Instructions
672 case Instruction::Call: executeCallInst ((CallInst*) I, SF); break;
673 case Instruction::PHINode: executePHINode ((PHINode*) I, SF); break;
674 case Instruction::Shl: executeShlInst ((ShiftInst*) I, SF); break;
675 case Instruction::Shr: executeShrInst ((ShiftInst*) I, SF); break;
676 case Instruction::Cast: executeCastInst ((CastInst*) I, SF); break;
672 case Instruction::Call: executeCallInst (cast (I), SF); break;
673 case Instruction::PHINode: executePHINode (cast (I), SF); break;
674 case Instruction::Shl: executeShlInst (cast(I), SF); break;
675 case Instruction::Shr: executeShrInst (cast(I), SF); break;
676 case Instruction::Cast: executeCastInst (cast (I), SF); break;
677677 default:
678678 cout << "Don't know how to execute this instruction!\n-->" << I;
679679 }
2020 #include "llvm/Target/MachineInstrInfo.h"
2121 #include "llvm/Target/MachineRegInfo.h"
2222 #include "llvm/Support/StringExtras.h"
23 #include "llvm/iOther.h"
2324 #include
2425
2526
539540 // Phi instructions are the only ones that produce a value but don't get
540541 // any non-dummy machine instructions. Return here as an optimization.
541542 //
542 if (defVMInstr->isPHINode())
543 if (isa(defVMInstr))
543544 return;
544545
545546 // Now add the graph edge for the appropriate machine instruction(s).
641642 SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
642643 const TargetMachine& target)
643644 {
644 if (instr->isPHINode())
645 if (isa(instr))
645646 return;
646647
647648 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
2525 #include "llvm/Method.h"
2626 #include "llvm/iTerminators.h"
2727 #include "llvm/iMemory.h"
28 #include "llvm/iOther.h"
2829 #include "llvm/ConstPoolVals.h"
2930 #include "llvm/BasicBlock.h"
3031 #include "llvm/CodeGen/MachineInstr.h"
5657
5758 // Distinguish special cases of some instructions such as Ret and Br
5859 //
59 if (opLabel == Instruction::Ret && ((ReturnInst*)I)->getReturnValue())
60 if (opLabel == Instruction::Ret && cast(I)->getReturnValue())
6061 {
6162 opLabel = RetValueOp; // ret(value) operation
6263 }
63 else if (opLabel == Instruction::Br && ! ((BranchInst*)I)->isUnconditional())
64 else if (opLabel ==Instruction::Br && !cast(I)->isUnconditional())
6465 {
6566 opLabel = BrCondOp; // br(cond) operation
6667 }
301302 InstrTreeNode* opTreeNode;
302303 if (isa(operand) && operand->use_size() == 1 &&
303304 cast(operand)->getParent() == instr->getParent() &&
304 ! instr->isPHINode() &&
305 !isa(instr) &&
305306 instr->getOpcode() != Instruction::Call)
306307 {
307308 // Recursively create a treeNode for it.
20092009 // Also, mark the operands of the Call as implicit operands
20102010 // of the machine instruction.
20112011 {
2012 CallInst* callInstr = (CallInst*) subtreeRoot->getInstruction();
2012 CallInst *callInstr = cast(subtreeRoot->getInstruction());
20132013 Method* callee = callInstr->getCalledMethod();
2014 assert(callInstr->getOpcode() == Instruction::Call);
20152014
20162015 Instruction* jmpAddrReg = new TmpInstruction(Instruction::UserOp1,
20172016 callee, NULL);
146146
147147 unsigned TargetData::getIndexedOffset(const Type *ptrTy,
148148 const vector &Idx) const {
149 const PointerType *PtrTy = ptrTy->castPointerType();
149 const PointerType *PtrTy = cast(ptrTy);
150150 unsigned Result = 0;
151151
152152 // Get the type pointed to...
153153 const Type *Ty = PtrTy->getValueType();
154154
155155 for (unsigned CurIDX = 0; CurIDX < Idx.size(); ++CurIDX) {
156 if (const StructType *STy = Ty->dyncastStructType()) {
156 if (const StructType *STy = dyn_cast(Ty)) {
157157 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
158158 unsigned FieldNo = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
159159
167167 // Update Ty to refer to current element
168168 Ty = STy->getElementTypes()[FieldNo];
169169
170 } else if (const ArrayType *ATy = Ty->dyncastArrayType()) {
170 } else if (const ArrayType *ATy = dyn_cast(Ty)) {
171171 assert(0 && "Loading from arrays not implemented yet!");
172172 } else {
173173 assert(0 && "Indexing type that is not struct or array?");
6262 // method by one level.
6363 //
6464 bool opt::InlineMethod(BasicBlock::iterator CIIt) {
65 assert((*CIIt)->getOpcode() == Instruction::Call &&
66 "InlineMethod only works on CallInst nodes!");
65 assert(isa(*CIIt) && "InlineMethod only works on CallInst nodes!");
6766 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
6867 assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
6968
70 CallInst *CI = (CallInst*)*CIIt;
69 CallInst *CI = cast(*CIIt);
7170 const Method *CalledMeth = CI->getCalledMethod();
7271 if (CalledMeth->isExternal()) return false; // Can't inline external method!
7372 Method *CurrentMeth = CI->getParent()->getParent();
151150 // Copy over the terminator now...
152151 switch (TI->getOpcode()) {
153152 case Instruction::Ret: {
154 const ReturnInst *RI = (const ReturnInst*)TI;
153 const ReturnInst *RI = cast(TI);
155154
156155 if (PHI) { // The PHI node should include this value!
157156 assert(RI->getReturnValue() && "Ret should have value!");
158157 assert(RI->getReturnValue()->getType() == PHI->getType() &&
159158 "Ret value not consistent in method!");
160 PHI->addIncoming((Value*)RI->getReturnValue(), (BasicBlock*)BB);
159 PHI->addIncoming((Value*)RI->getReturnValue(), cast(BB));
161160 }
162161
163162 // Add a branch to the code that was after the original Call.
235234
236235 static inline bool DoMethodInlining(BasicBlock *BB) {
237236 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
238 if ((*I)->getOpcode() == Instruction::Call) {
237 if (CallInst *CI = dyn_cast(*I)) {
239238 // Check to see if we should inline this method
240 CallInst *CI = (CallInst*)*I;
241239 Method *M = CI->getCalledMethod();
242240 if (ShouldInlineMethod(CI, M))
243241 return InlineMethod(I);
1313 #include "llvm/Support/DepthFirstIterator.h"
1414 #include "llvm/Analysis/Writer.h"
1515 #include "llvm/iTerminators.h"
16 #include "llvm/iOther.h"
1617 #include
1718 #include
1819
170171 set VisitedBlocks;
171172 BasicBlock *EntryBlock = fixupCFG(M->front(), VisitedBlocks, AliveBlocks);
172173 if (EntryBlock && EntryBlock != M->front()) {
173 if (EntryBlock->front()->isPHINode()) {
174 if (isa(EntryBlock->front())) {
174175 // Cannot make the first block be a block with a PHI node in it! Instead,
175176 // strip the first basic block of the method to contain no instructions,
176177 // then add a simple branch to the "real" entry node...
177178 //
178179 BasicBlock *E = M->front();
179 if (!E->front()->isTerminator() || // Check for an actual change...
180 ((TerminatorInst*)E->front())->getNumSuccessors() != 1 ||
181 ((TerminatorInst*)E->front())->getSuccessor(0) != EntryBlock) {
180 if (!isa(E->front()) || // Check for an actual change...
181 cast(E->front())->getNumSuccessors() != 1 ||
182 cast(E->front())->getSuccessor(0) != EntryBlock) {
182183 E->getInstList().delete_all(); // Delete all instructions in block
183184 E->getInstList().push_back(new BranchInst(EntryBlock));
184185 MadeChanges = true;
8181 //
8282 bool opt::ConstantFoldTerminator(TerminatorInst *T) {
8383 // Branch - See if we are conditional jumping on constant
84 if (T->getOpcode() == Instruction::Br) {
85 BranchInst *BI = (BranchInst*)T;
84 if (BranchInst *BI = dyn_cast(T)) {
8685 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
8786 BasicBlock *Dest1 = cast(BI->getOperand(0));
8887 BasicBlock *Dest2 = cast(BI->getOperand(1));
135134 inline static bool
136135 ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
137136 Instruction *Inst = *II;
138 if (Inst->isBinaryOp()) {
137 if (BinaryOperator *BInst = dyn_cast(Inst)) {
139138 ConstPoolVal *D1 = dyn_cast(Inst->getOperand(0));
140139 ConstPoolVal *D2 = dyn_cast(Inst->getOperand(1));
141140
142141 if (D1 && D2)
143 return ConstantFoldBinaryInst(M, II, (BinaryOperator*)Inst, D1, D2);
144
145 } else if (Inst->isUnaryOp()) {
146 ConstPoolVal *D = dyn_cast(Inst->getOperand(0));
147 if (D) return ConstantFoldUnaryInst(M, II, (UnaryOperator*)Inst, D);
148 } else if (Inst->isTerminator()) {
149 return opt::ConstantFoldTerminator((TerminatorInst*)Inst);
150
151 } else if (Inst->isPHINode()) {
152 PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand
153 // Then replace it directly with that operand.
142 return ConstantFoldBinaryInst(M, II, cast(Inst), D1, D2);
143
144 } else if (UnaryOperator *UInst = dyn_cast(Inst)) {
145 ConstPoolVal *D = dyn_cast(UInst->getOperand(0));
146 if (D) return ConstantFoldUnaryInst(M, II, UInst, D);
147 } else if (TerminatorInst *TInst = dyn_cast(Inst)) {
148 return opt::ConstantFoldTerminator(TInst);
149
150 } else if (PHINode *PN = dyn_cast(Inst)) {
151 // If it's a PHI node and only has one operand
152 // Then replace it directly with that operand.
154153 assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
155154 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
156155 Value *V = PN->getOperand(0);
8383 return false; // More than one predecessor...
8484
8585 Instruction *I = BB->front();
86 if (!I->isPHINode()) return false; // No PHI nodes
86 if (!isa(I)) return false; // No PHI nodes
8787
8888 //cerr << "Killing PHIs from " << BB;
8989 //cerr << "Pred #0 = " << *BB->pred_begin();
9191 //cerr << "Method == " << BB->getParent();
9292
9393 do {
94 PHINode *PN = (PHINode*)I;
94 PHINode *PN = cast(I);
9595 assert(PN->getNumOperands() == 2 && "PHI node should only have one value!");
9696 Value *V = PN->getOperand(0);
9797
9999 delete BB->getInstList().remove(BB->begin());
100100
101101 I = BB->front();
102 } while (I->isPHINode());
102 } while (isa(I));
103103
104104 return true; // Yes, we nuked at least one phi node
105105 }
119119 // Assumption: BB is the single predecessor of Succ.
120120 //
121121 static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
122 assert(Succ->front()->isPHINode() && "Only works on PHId BBs!");
122 assert(isa(Succ->front()) && "Only works on PHId BBs!");
123123
124124 // If there is more than one predecessor, and there are PHI nodes in
125125 // the successor, then we need to add incoming edges for the PHI nodes
128128
129129 BasicBlock::iterator I = Succ->begin();
130130 do { // Loop over all of the PHI nodes in the successor BB
131 PHINode *PN = (PHINode*)*I;
131 PHINode *PN = cast(*I);
132132 Value *OldVal = PN->removeIncomingValue(BB);
133133 assert(OldVal && "No entry in PHI for Pred BB!");
134134
139139 }
140140
141141 ++I;
142 } while ((*I)->isPHINode());
142 } while (isa(*I));
143143 }
144144
145145
197197 //cerr << "Killing Trivial BB: \n" << BB;
198198
199199 if (Succ != BB) { // Arg, don't hurt infinite loops!
200 if (Succ->front()->isPHINode()) {
200 if (isa(Succ->front())) {
201201 // If our successor has PHI nodes, then we need to update them to
202202 // include entries for BB's predecessors, not for BB itself.
203203 //
4040 if (!isa(V))
4141 return true; // Constants and arguments are always loop invariant
4242
43 BasicBlock *ValueBlock = ((Instruction*)V)->getParent();
43 BasicBlock *ValueBlock = cast(V)->getParent();
4444 assert(ValueBlock && "Instruction not embedded in basic block!");
4545
4646 // For now, only consider values from outside of the interval, regardless of
7979 switch (I->getOpcode()) { // Handle each instruction seperately
8080 case Instruction::Add:
8181 case Instruction::Sub: {
82 Value *SubV1 = ((BinaryOperator*)I)->getOperand(0);
83 Value *SubV2 = ((BinaryOperator*)I)->getOperand(1);
82 Value *SubV1 = cast(I)->getOperand(0);
83 Value *SubV2 = cast(I)->getOperand(1);
8484 LIVType SubLIVType1 = isLinearInductionVariableH(Int, SubV1, PN);
8585 if (SubLIVType1 == isOther) return isOther; // Early bailout
8686 LIVType SubLIVType2 = isLinearInductionVariableH(Int, SubV2, PN);
143143
144144 Value *StepExpr = PN->getIncomingValue(1);
145145 if (!isa(StepExpr) ||
146 ((Instruction*)StepExpr)->getOpcode() != Instruction::Add)
146 cast(StepExpr)->getOpcode() != Instruction::Add)
147147 return false;
148148
149 BinaryOperator *I = (BinaryOperator*)StepExpr;
150 assert(isa(I->getOperand(0)) &&
151 ((Instruction*)I->getOperand(0))->isPHINode() &&
149 BinaryOperator *I = cast(StepExpr);
150 assert(isa(I->getOperand(0)) &&
152151 "PHI node should be first operand of ADD instruction!");
153152
154153 // Get the right hand side of the ADD node. See if it is a constant 1.
224223 // Insert the Add instruction as the first (non-phi) instruction in the
225224 // header node's basic block.
226225 BasicBlock::iterator I = IL.begin();
227 while ((*I)->isPHINode()) ++I;
226 while (isa(*I)) ++I;
228227 IL.insert(I, AddNode);
229228 return PN;
230229 }
255254 BasicBlock *Header = Int->getHeaderNode();
256255 // Loop over all of the PHI nodes in the interval header...
257256 for (BasicBlock::iterator I = Header->begin(), E = Header->end();
258 I != E && (*I)->isPHINode(); ++I) {
259 PHINode *PN = (PHINode*)*I;
257 I != E && isa(*I); ++I) {
258 PHINode *PN = cast(*I);
260259 if (PN->getNumIncomingValues() != 2) { // These should be eliminated by now.
261260 cerr << "Found interval header with more than 2 predecessors! Ignoring\n";
262261 return false; // Todo, make an assertion.
269269 MadeChanges = true;
270270 continue; // Skip the ++II at the end of the loop here...
271271 } else if (Inst->isTerminator()) {
272 MadeChanges |= opt::ConstantFoldTerminator((TerminatorInst*)Inst);
272 MadeChanges |= opt::ConstantFoldTerminator(cast(Inst));
273273 }
274274
275275 ++II;
311311 // Handle PHI nodes...
312312 //
313313 case Instruction::PHINode: {
314 PHINode *PN = (PHINode*)I;
314 PHINode *PN = cast(I);
315315 unsigned NumValues = PN->getNumIncomingValues(), i;
316316 InstVal *OperandIV = 0;
317317
379379 //
380380 case Instruction::Ret: return; // Method return doesn't affect anything
381381 case Instruction::Br: { // Handle conditional branches...
382 BranchInst *BI = (BranchInst*)I;
382 BranchInst *BI = cast(I);
383383 if (BI->isUnconditional())
384384 return; // Unconditional branches are already handled!
385385
390390 markExecutable(BI->getSuccessor(1));
391391 } else if (BCValue.isConstant()) {
392392 // Constant condition variables mean the branch can only go a single way.
393 ConstPoolBool *CPB = (ConstPoolBool*)BCValue.getConstant();
393 ConstPoolBool *CPB = cast(BCValue.getConstant());
394394 if (CPB->getValue()) // If the branch condition is TRUE...
395395 markExecutable(BI->getSuccessor(0));
396396 else // Else if the br cond is FALSE...
400400 }
401401
402402 case Instruction::Switch: {
403 SwitchInst *SI = (SwitchInst*)I;
403 SwitchInst *SI = cast(I);
404404 InstVal &SCValue = getValueState(SI->getCondition());
405405 if (SCValue.isOverdefined()) { // Overdefined condition? All dests are exe
406406 for(unsigned i = 0; BasicBlock *Succ = SI->getSuccessor(i); ++i)
431431 // Also treated as unary here, are cast instructions and getelementptr
432432 // instructions on struct* operands.
433433 //
434 if (I->isUnaryOp() || I->getOpcode() == Instruction::Cast ||
435 (I->getOpcode() == Instruction::GetElementPtr &&
436 ((GetElementPtrInst*)I)->isStructSelector())) {
434 if (isa(I) || isa(I) ||
435 (isa(I) &&
436 cast(I)->isStructSelector())) {
437437
438438 Value *V = I->getOperand(0);
439439 InstVal &VState = getValueState(V);
457457 //===-----------------------------------------------------------------===//
458458 // Handle Binary instructions...
459459 //
460 if (I->isBinaryOp() || I->getOpcode() == Instruction::Shl ||
461 I->getOpcode() == Instruction::Shr) {
460 if (isa(I) || isa(I)) {
462461 Value *V1 = I->getOperand(0);
463462 Value *V2 = I->getOperand(1);
464463
191191
192192
193193 // Finish printing arguments...
194 const MethodType *MT = (const MethodType*)M->getType();
194 const MethodType *MT = cast(M->getType());
195195 if (MT->isVarArg()) {
196196 if (MT->getParamTypes().size()) Out << ", ";
197197 Out << "..."; // Output varargs portion of signature!
286286 writeOperand(I->getOperand(op+1), true);
287287 }
288288 Out << "\n\t]";
289 } else if (I->isPHINode()) {
289 } else if (isa(I)) {
290290 Out << " " << Operand->getType();
291291
292292 Out << " ["; writeOperand(Operand, false); Out << ",";
310310 Out << " )";
311311 } else if (I->getOpcode() == Instruction::Malloc ||
312312 I->getOpcode() == Instruction::Alloca) {
313 Out << " " << ((const PointerType*)I->getType())->getValueType();
313 Out << " " << cast(I->getType())->getValueType();
314314 if (I->getNumOperands()) {
315315 Out << ",";
316316 writeOperand(I->getOperand(0), true);
5353 TerminatorInst *BasicBlock::getTerminator() {
5454 if (InstList.empty()) return 0;
5555 Instruction *T = InstList.back();
56 if (T->isTerminator()) return (TerminatorInst*)T;
56 if (isa(T)) return cast(T);
5757 return 0;
5858 }
5959
6060 const TerminatorInst *const BasicBlock::getTerminator() const {
6161 if (InstList.empty()) return 0;
62 const Instruction *T = InstList.back();
63 if (T->isTerminator()) return (TerminatorInst*)T;
62 if (const TerminatorInst *TI = dyn_cast(InstList.back()))
63 return TI;
6464 return 0;
6565 }
6666
9191 void BasicBlock::removePredecessor(BasicBlock *Pred) {
9292 assert(find(pred_begin(), pred_end(), Pred) != pred_end() &&
9393 "removePredecessor: BB is not a predecessor!");
94 if (!front()->isPHINode()) return; // Quick exit.
94 if (!isa(front())) return; // Quick exit.
9595
9696 pred_iterator PI(pred_begin()), EI(pred_end());
9797 unsigned max_idx;
104104 // altogether.
105105 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
106106 if (max_idx <= 2) { // <= Two predecessors BEFORE I remove one?
107 while (front()->isPHINode()) { // Yup, loop through and nuke the PHI nodes
108 PHINode *PN = (PHINode*)front();
107 // Yup, loop through and nuke the PHI nodes
108 while (PHINode *PN = dyn_cast(front())) {
109109 PN->removeIncomingValue(Pred); // Remove the predecessor first...
110110
111111 assert(PN->getNumIncomingValues() == max_idx-1 &&
120120 // Okay, now we know that we need to remove predecessor #pred_idx from all
121121 // PHI nodes. Iterate over each PHI node fixing them up
122122 iterator II(begin());
123 for (; (*II)->isPHINode(); ++II) {
124 PHINode *PN = (PHINode*)*II;
125 PN->removeIncomingValue(Pred);
126 }
123 for (; isa(*II); ++II)
124 cast(*II)->removeIncomingValue(Pred);
127125 }
128126 }
129127
4444 case Type::DoubleTyID: return ConstPoolFP::get(Ty, 0);
4545
4646 case Type::PointerTyID:
47 return ConstPoolPointer::getNullPointer(Ty->castPointerType());
47 return ConstPoolPointer::getNullPointer(cast(Ty));
4848 default:
4949 return 0;
5050 }
5151 }
5252
53 bool ConstPoolInt::isa(const ConstPoolVal *CPV) {
53 bool ConstPoolInt::classof(const ConstPoolVal *CPV) {
5454 return CPV->getType()->isIntegral();
5555 }
5656
191191 if (!Ty->isAbstract() && !Ty->isRecursive() && // Base case for the recursion
192192 Ty->getDescription().size()) {
193193 Result = Ty->getDescription(); // Primitive = leaf type
194 } else if (Ty->isOpaqueType()) { // Base case for the recursion
194 } else if (isa(Ty)) { // Base case for the recursion
195195 Result = Ty->getDescription(); // Opaque = leaf type
196196 isAbstract = true; // This whole type is abstract!
197197 } else {
211211
212212 switch (Ty->getPrimitiveID()) {
213213 case Type::MethodTyID: {
214 const MethodType *MTy = (const MethodType*)Ty;
214 const MethodType *MTy = cast(Ty);
215215 Result = getTypeProps(MTy->getReturnType(), TypeStack,
216216 isAbstract, isRecursive)+" (";
217217 for (MethodType::ParamTypes::const_iterator
229229 break;
230230 }
231231 case Type::StructTyID: {
232 const StructType *STy = (const StructType*)Ty;
232 const StructType *STy = cast(Ty);
233233 Result = "{ ";
234234 for (StructType::ElementTypes::const_iterator
235235 I = STy->getElementTypes().begin(),
242242 break;
243243 }
244244 case Type::PointerTyID: {
245 const PointerType *PTy = (const PointerType*)Ty;
245 const PointerType *PTy = cast(Ty);
246246 Result = getTypeProps(PTy->getValueType(), TypeStack,
247247 isAbstract, isRecursive) + " *";
248248 break;
249249 }
250250 case Type::ArrayTyID: {
251 const ArrayType *ATy = (const ArrayType*)Ty;
251 const ArrayType *ATy = cast(Ty);
252252 int NumElements = ATy->getNumElements();
253253 Result = "[";
254254 if (NumElements != -1) Result += itostr(NumElements) + " x ";
318318 // algorithm is the fact that arraytypes have sizes that differentiates types,
319319 // consider this now.
320320 if (Ty->isArrayType())
321 if (((const ArrayType*)Ty)->getNumElements() !=
322 ((const ArrayType*)Ty2)->getNumElements()) return false;
321 if (cast(Ty)->getNumElements() !=
322 cast(Ty2)->getNumElements()) return false;
323323
324324 return I == IE && I2 == IE2; // Types equal if both iterators are done
325325 }