blob: 0d40ee7cf96905c2423de584dfa4f7f8a96ce6dd [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Support/CallSite.h"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Chris Lattner1c12a882006-06-21 16:53:47 +000037
38
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000039//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000040// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000044 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000045 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
46}
47
48TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
51}
52
Chris Lattner1c12a882006-06-21 16:53:47 +000053// Out of line virtual method, so the vtable, etc has a home.
54TerminatorInst::~TerminatorInst() {
55}
56
57// Out of line virtual method, so the vtable, etc has a home.
58UnaryInstruction::~UnaryInstruction() {
59}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000060
61
62//===----------------------------------------------------------------------===//
63// PHINode Class
64//===----------------------------------------------------------------------===//
65
66PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
74 }
75}
76
77PHINode::~PHINode() {
78 delete [] OperandList;
79}
80
81// removeIncomingValue - Remove an incoming value. This is useful if a
82// predecessor basic block is deleted.
83Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
88
89 // Move everything after this operand down.
90 //
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
97 }
98
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
103
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
109 }
110 return Removed;
111}
112
113/// resizeOperands - resize operands - This adjusts the length of the operands
114/// list according to the following behavior:
115/// 1. If NumOps == 0, grow the operand list in response to a push_back style
116/// of operation. This grows the number of ops by 1.5 times.
117/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118/// 3. If NumOps == NumOperands, trim the reserved space.
119///
120void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 }
132
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
139 }
140 delete [] OldOps;
141 OperandList = NewOps;
142}
143
Nate Begemanb3923212005-08-04 23:24:19 +0000144/// hasConstantValue - If the specified PHI node always merges together the same
145/// value, return the value, otherwise return null.
146///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000147Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
157 //
158 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000159 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
168
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
172 //
173 if (InVal == 0) InVal = UndefValue::get(getType());
174
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 return 0; // Cannot guarantee that InVal dominates this PHINode.
185
Nate Begemanb3923212005-08-04 23:24:19 +0000186 // All of the incoming values are the same, return the value now.
187 return InVal;
188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190
191//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192// CallInst Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195CallInst::~CallInst() {
196 delete [] OperandList;
197}
198
199void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
200 NumOperands = Params.size()+1;
201 Use *OL = OperandList = new Use[Params.size()+1];
202 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
206
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207 assert((Params.size() == FTy->getNumParams() ||
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000208 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000209 "Calling a function with bad signature!");
210 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
211 assert((i >= FTy->getNumParams() ||
212 FTy->getParamType(i) == Params[i]->getType()) &&
213 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000214 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000215 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000216}
217
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
219 NumOperands = 3;
220 Use *OL = OperandList = new Use[3];
221 OL[0].init(Func, this);
222 OL[1].init(Actual1, this);
223 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000224
225 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
227
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000229 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000231 assert((0 >= FTy->getNumParams() ||
232 FTy->getParamType(0) == Actual1->getType()) &&
233 "Calling a function with a bad signature!");
234 assert((1 >= FTy->getNumParams() ||
235 FTy->getParamType(1) == Actual2->getType()) &&
236 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237}
238
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000239void CallInst::init(Value *Func, Value *Actual) {
240 NumOperands = 2;
241 Use *OL = OperandList = new Use[2];
242 OL[0].init(Func, this);
243 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000244
245 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
247
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000248 assert((FTy->getNumParams() == 1 ||
249 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000251 assert((0 == FTy->getNumParams() ||
252 FTy->getParamType(0) == Actual->getType()) &&
253 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254}
255
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000256void CallInst::init(Value *Func) {
257 NumOperands = 1;
258 Use *OL = OperandList = new Use[1];
259 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260
261 const FunctionType *MTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
263
264 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
265}
266
Misha Brukmanb1c93172005-04-21 23:48:37 +0000267CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
268 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
270 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000271 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 init(Func, Params);
273}
274
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
276 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
278 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 init(Func, Params);
281}
282
283CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
284 const std::string &Name, Instruction *InsertBefore)
285 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
286 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288 init(Func, Actual1, Actual2);
289}
290
291CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
292 const std::string &Name, BasicBlock *InsertAtEnd)
293 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
294 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000295 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 init(Func, Actual1, Actual2);
297}
298
299CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
300 Instruction *InsertBefore)
301 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
302 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000303 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 init(Func, Actual);
305}
306
307CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
308 BasicBlock *InsertAtEnd)
309 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
310 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000311 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 init(Func, Actual);
313}
314
315CallInst::CallInst(Value *Func, const std::string &Name,
316 Instruction *InsertBefore)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000319 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func);
321}
322
323CallInst::CallInst(Value *Func, const std::string &Name,
324 BasicBlock *InsertAtEnd)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000327 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 init(Func);
329}
330
Misha Brukmanb1c93172005-04-21 23:48:37 +0000331CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000332 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
333 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000334 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000335 Use *OL = OperandList;
336 Use *InOL = CI.OperandList;
337 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
338 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339}
340
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341
342//===----------------------------------------------------------------------===//
343// InvokeInst Implementation
344//===----------------------------------------------------------------------===//
345
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000346InvokeInst::~InvokeInst() {
347 delete [] OperandList;
348}
349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000351 const std::vector<Value*> &Params) {
352 NumOperands = 3+Params.size();
353 Use *OL = OperandList = new Use[3+Params.size()];
354 OL[0].init(Fn, this);
355 OL[1].init(IfNormal, this);
356 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000357 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359
360 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000361 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363
Chris Lattner667a0562006-05-03 00:48:22 +0000364 for (unsigned i = 0, e = Params.size(); i != e; i++) {
365 assert((i >= FTy->getNumParams() ||
366 FTy->getParamType(i) == Params[i]->getType()) &&
367 "Invoking a function with a bad signature!");
368
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000369 OL[i+3].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000370 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371}
372
373InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
374 BasicBlock *IfException,
375 const std::vector<Value*> &Params,
376 const std::string &Name, Instruction *InsertBefore)
377 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
378 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000379 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380 init(Fn, IfNormal, IfException, Params);
381}
382
383InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
384 BasicBlock *IfException,
385 const std::vector<Value*> &Params,
386 const std::string &Name, BasicBlock *InsertAtEnd)
387 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
388 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000389 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390 init(Fn, IfNormal, IfException, Params);
391}
392
Misha Brukmanb1c93172005-04-21 23:48:37 +0000393InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000394 : TerminatorInst(II.getType(), Instruction::Invoke,
395 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000396 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000397 Use *OL = OperandList, *InOL = II.OperandList;
398 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
399 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000400}
401
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
403 return getSuccessor(idx);
404}
405unsigned InvokeInst::getNumSuccessorsV() const {
406 return getNumSuccessors();
407}
408void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
409 return setSuccessor(idx, B);
410}
411
412
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000413//===----------------------------------------------------------------------===//
414// ReturnInst Implementation
415//===----------------------------------------------------------------------===//
416
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000417void ReturnInst::init(Value *retVal) {
418 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000419 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000420 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000421 NumOperands = 1;
422 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000423 }
424}
425
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000426unsigned ReturnInst::getNumSuccessorsV() const {
427 return getNumSuccessors();
428}
429
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000430// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
431// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000432void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000433 assert(0 && "ReturnInst has no successors!");
434}
435
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
437 assert(0 && "ReturnInst has no successors!");
438 abort();
439 return 0;
440}
441
442
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443//===----------------------------------------------------------------------===//
444// UnwindInst Implementation
445//===----------------------------------------------------------------------===//
446
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000447unsigned UnwindInst::getNumSuccessorsV() const {
448 return getNumSuccessors();
449}
450
451void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000452 assert(0 && "UnwindInst has no successors!");
453}
454
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
456 assert(0 && "UnwindInst has no successors!");
457 abort();
458 return 0;
459}
460
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000461//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000462// UnreachableInst Implementation
463//===----------------------------------------------------------------------===//
464
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000465unsigned UnreachableInst::getNumSuccessorsV() const {
466 return getNumSuccessors();
467}
468
469void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
470 assert(0 && "UnwindInst has no successors!");
471}
472
473BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
474 assert(0 && "UnwindInst has no successors!");
475 abort();
476 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000477}
478
479//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000480// BranchInst Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000483void BranchInst::AssertOK() {
484 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000485 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000486 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000487}
488
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000489BranchInst::BranchInst(const BranchInst &BI) :
490 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
491 OperandList[0].init(BI.getOperand(0), this);
492 if (BI.getNumOperands() != 1) {
493 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
494 OperandList[1].init(BI.getOperand(1), this);
495 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496 }
497}
498
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000499BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
500 return getSuccessor(idx);
501}
502unsigned BranchInst::getNumSuccessorsV() const {
503 return getNumSuccessors();
504}
505void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
506 setSuccessor(idx, B);
507}
508
509
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000510//===----------------------------------------------------------------------===//
511// AllocationInst Implementation
512//===----------------------------------------------------------------------===//
513
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000514static Value *getAISize(Value *Amt) {
515 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000516 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000517 else {
518 assert(!isa<BasicBlock>(Amt) &&
519 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000520 assert(Amt->getType() == Type::Int32Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000521 "Malloc/Allocation array size != UIntTy!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000522 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000523 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000524}
525
Misha Brukmanb1c93172005-04-21 23:48:37 +0000526AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000527 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000529 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000530 Name, InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000531 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000533}
534
Misha Brukmanb1c93172005-04-21 23:48:37 +0000535AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000536 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000539 Name, InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000540 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000541 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542}
543
Chris Lattner1c12a882006-06-21 16:53:47 +0000544// Out of line virtual method, so the vtable, etc has a home.
545AllocationInst::~AllocationInst() {
546}
547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000548bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000549 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
550 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000551 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000552}
553
554const Type *AllocationInst::getAllocatedType() const {
555 return getType()->getElementType();
556}
557
558AllocaInst::AllocaInst(const AllocaInst &AI)
559 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000560 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561}
562
563MallocInst::MallocInst(const MallocInst &MI)
564 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000565 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566}
567
568//===----------------------------------------------------------------------===//
569// FreeInst Implementation
570//===----------------------------------------------------------------------===//
571
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572void FreeInst::AssertOK() {
573 assert(isa<PointerType>(getOperand(0)->getType()) &&
574 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575}
576
577FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
579 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000580}
581
582FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000583 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
584 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585}
586
587
588//===----------------------------------------------------------------------===//
589// LoadInst Implementation
590//===----------------------------------------------------------------------===//
591
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000593 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000594 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000595}
596
597LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000598 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000599 Load, Ptr, Name, InsertBef) {
600 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602}
603
604LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000606 Load, Ptr, Name, InsertAE) {
607 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
611LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
612 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000614 Load, Ptr, Name, InsertBef) {
615 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000616 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617}
618
619LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
620 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000621 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000622 Load, Ptr, Name, InsertAE) {
623 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000625}
626
627
628//===----------------------------------------------------------------------===//
629// StoreInst Implementation
630//===----------------------------------------------------------------------===//
631
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632void StoreInst::AssertOK() {
633 assert(isa<PointerType>(getOperand(1)->getType()) &&
634 "Ptr must have pointer type!");
635 assert(getOperand(0)->getType() ==
636 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000637 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000640
641StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000642 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643 Ops[0].init(val, this);
644 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000645 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646 AssertOK();
647}
648
649StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000650 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651 Ops[0].init(val, this);
652 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000653 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654 AssertOK();
655}
656
Misha Brukmanb1c93172005-04-21 23:48:37 +0000657StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000659 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660 Ops[0].init(val, this);
661 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000662 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663 AssertOK();
664}
665
Misha Brukmanb1c93172005-04-21 23:48:37 +0000666StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000667 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000668 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669 Ops[0].init(val, this);
670 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000671 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000673}
674
675//===----------------------------------------------------------------------===//
676// GetElementPtrInst Implementation
677//===----------------------------------------------------------------------===//
678
679// checkType - Simple wrapper function to give a better assertion failure
680// message on bad indexes for a gep instruction.
681//
682static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000683 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684 return Ty;
685}
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
688 NumOperands = 1+Idx.size();
689 Use *OL = OperandList = new Use[NumOperands];
690 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000691
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
693 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
696void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697 NumOperands = 3;
698 Use *OL = OperandList = new Use[3];
699 OL[0].init(Ptr, this);
700 OL[1].init(Idx0, this);
701 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702}
703
Chris Lattner82981202005-05-03 05:43:30 +0000704void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
705 NumOperands = 2;
706 Use *OL = OperandList = new Use[2];
707 OL[0].init(Ptr, this);
708 OL[1].init(Idx, this);
709}
710
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000711GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000712 const std::string &Name, Instruction *InBe)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
714 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716 init(Ptr, Idx);
717}
718
719GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000720 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
722 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724 init(Ptr, Idx);
725}
726
Chris Lattner82981202005-05-03 05:43:30 +0000727GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
728 const std::string &Name, Instruction *InBe)
729 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
730 GetElementPtr, 0, 0, Name, InBe) {
731 init(Ptr, Idx);
732}
733
734GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
735 const std::string &Name, BasicBlock *IAE)
736 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
737 GetElementPtr, 0, 0, Name, IAE) {
738 init(Ptr, Idx);
739}
740
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
742 const std::string &Name, Instruction *InBe)
743 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
744 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746 init(Ptr, Idx0, Idx1);
747}
748
749GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000750 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
752 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754 init(Ptr, Idx0, Idx1);
755}
756
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757GetElementPtrInst::~GetElementPtrInst() {
758 delete[] OperandList;
759}
760
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000761// getIndexedType - Returns the type of the element that would be loaded with
762// a load instruction with the specified parameters.
763//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000764// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765// pointer type.
766//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000767const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768 const std::vector<Value*> &Idx,
769 bool AllowCompositeLeaf) {
770 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
771
772 // Handle the special case of the empty set index set...
773 if (Idx.empty())
774 if (AllowCompositeLeaf ||
775 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
776 return cast<PointerType>(Ptr)->getElementType();
777 else
778 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000779
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780 unsigned CurIdx = 0;
781 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
782 if (Idx.size() == CurIdx) {
783 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
784 return 0; // Can't load a whole structure or array!?!?
785 }
786
787 Value *Index = Idx[CurIdx++];
788 if (isa<PointerType>(CT) && CurIdx != 1)
789 return 0; // Can only index into pointer types at the first index!
790 if (!CT->indexValid(Index)) return 0;
791 Ptr = CT->getTypeAtIndex(Index);
792
793 // If the new type forwards to another type, then it is in the middle
794 // of being refined to another type (and hence, may have dropped all
795 // references to what it was using before). So, use the new forwarded
796 // type.
797 if (const Type * Ty = Ptr->getForwardedType()) {
798 Ptr = Ty;
799 }
800 }
801 return CurIdx == Idx.size() ? Ptr : 0;
802}
803
Misha Brukmanb1c93172005-04-21 23:48:37 +0000804const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805 Value *Idx0, Value *Idx1,
806 bool AllowCompositeLeaf) {
807 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
808 if (!PTy) return 0; // Type isn't a pointer type!
809
810 // Check the pointer index.
811 if (!PTy->indexValid(Idx0)) return 0;
812
813 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
814 if (!CT || !CT->indexValid(Idx1)) return 0;
815
816 const Type *ElTy = CT->getTypeAtIndex(Idx1);
817 if (AllowCompositeLeaf || ElTy->isFirstClassType())
818 return ElTy;
819 return 0;
820}
821
Chris Lattner82981202005-05-03 05:43:30 +0000822const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
823 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
824 if (!PTy) return 0; // Type isn't a pointer type!
825
826 // Check the pointer index.
827 if (!PTy->indexValid(Idx)) return 0;
828
Chris Lattnerc2233332005-05-03 16:44:45 +0000829 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000830}
831
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000832//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000833// ExtractElementInst Implementation
834//===----------------------------------------------------------------------===//
835
836ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000837 const std::string &Name,
838 Instruction *InsertBef)
Robert Bocchino23004482006-01-10 19:05:34 +0000839 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
840 ExtractElement, Ops, 2, Name, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000841 assert(isValidOperands(Val, Index) &&
842 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000843 Ops[0].init(Val, this);
844 Ops[1].init(Index, this);
845}
846
Chris Lattner65511ff2006-10-05 06:24:58 +0000847ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
848 const std::string &Name,
849 Instruction *InsertBef)
850 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
851 ExtractElement, Ops, 2, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000852 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000853 assert(isValidOperands(Val, Index) &&
854 "Invalid extractelement instruction operands!");
855 Ops[0].init(Val, this);
856 Ops[1].init(Index, this);
857}
858
859
Robert Bocchino23004482006-01-10 19:05:34 +0000860ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000861 const std::string &Name,
862 BasicBlock *InsertAE)
Robert Bocchino23004482006-01-10 19:05:34 +0000863 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
864 ExtractElement, Ops, 2, Name, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000865 assert(isValidOperands(Val, Index) &&
866 "Invalid extractelement instruction operands!");
867
Robert Bocchino23004482006-01-10 19:05:34 +0000868 Ops[0].init(Val, this);
869 Ops[1].init(Index, this);
870}
871
Chris Lattner65511ff2006-10-05 06:24:58 +0000872ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
873 const std::string &Name,
874 BasicBlock *InsertAE)
875 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
876 ExtractElement, Ops, 2, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000877 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000878 assert(isValidOperands(Val, Index) &&
879 "Invalid extractelement instruction operands!");
880
881 Ops[0].init(Val, this);
882 Ops[1].init(Index, this);
883}
884
885
Chris Lattner54865b32006-04-08 04:05:48 +0000886bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000887 if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000888 return false;
889 return true;
890}
891
892
Robert Bocchino23004482006-01-10 19:05:34 +0000893//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +0000894// InsertElementInst Implementation
895//===----------------------------------------------------------------------===//
896
Chris Lattner0875d942006-04-14 22:20:32 +0000897InsertElementInst::InsertElementInst(const InsertElementInst &IE)
898 : Instruction(IE.getType(), InsertElement, Ops, 3) {
899 Ops[0].init(IE.Ops[0], this);
900 Ops[1].init(IE.Ops[1], this);
901 Ops[2].init(IE.Ops[2], this);
902}
Chris Lattner54865b32006-04-08 04:05:48 +0000903InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000904 const std::string &Name,
905 Instruction *InsertBef)
Chris Lattner54865b32006-04-08 04:05:48 +0000906 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
907 assert(isValidOperands(Vec, Elt, Index) &&
908 "Invalid insertelement instruction operands!");
909 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000910 Ops[1].init(Elt, this);
911 Ops[2].init(Index, this);
912}
913
Chris Lattner65511ff2006-10-05 06:24:58 +0000914InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
915 const std::string &Name,
916 Instruction *InsertBef)
917 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000918 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000919 assert(isValidOperands(Vec, Elt, Index) &&
920 "Invalid insertelement instruction operands!");
921 Ops[0].init(Vec, this);
922 Ops[1].init(Elt, this);
923 Ops[2].init(Index, this);
924}
925
926
Chris Lattner54865b32006-04-08 04:05:48 +0000927InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000928 const std::string &Name,
929 BasicBlock *InsertAE)
Chris Lattner54865b32006-04-08 04:05:48 +0000930 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
931 assert(isValidOperands(Vec, Elt, Index) &&
932 "Invalid insertelement instruction operands!");
933
934 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000935 Ops[1].init(Elt, this);
936 Ops[2].init(Index, this);
937}
938
Chris Lattner65511ff2006-10-05 06:24:58 +0000939InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
940 const std::string &Name,
941 BasicBlock *InsertAE)
942: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000943 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000944 assert(isValidOperands(Vec, Elt, Index) &&
945 "Invalid insertelement instruction operands!");
946
947 Ops[0].init(Vec, this);
948 Ops[1].init(Elt, this);
949 Ops[2].init(Index, this);
950}
951
Chris Lattner54865b32006-04-08 04:05:48 +0000952bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
953 const Value *Index) {
954 if (!isa<PackedType>(Vec->getType()))
955 return false; // First operand of insertelement must be packed type.
956
957 if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
958 return false;// Second operand of insertelement must be packed element type.
959
Reid Spencer8d9336d2006-12-31 05:26:44 +0000960 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000961 return false; // Third operand of insertelement must be uint.
962 return true;
963}
964
965
Robert Bocchinoca27f032006-01-17 20:07:22 +0000966//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000967// ShuffleVectorInst Implementation
968//===----------------------------------------------------------------------===//
969
Chris Lattner0875d942006-04-14 22:20:32 +0000970ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
971 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
972 Ops[0].init(SV.Ops[0], this);
973 Ops[1].init(SV.Ops[1], this);
974 Ops[2].init(SV.Ops[2], this);
975}
976
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000977ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
978 const std::string &Name,
979 Instruction *InsertBefore)
980 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
981 assert(isValidOperands(V1, V2, Mask) &&
982 "Invalid shuffle vector instruction operands!");
983 Ops[0].init(V1, this);
984 Ops[1].init(V2, this);
985 Ops[2].init(Mask, this);
986}
987
988ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
989 const std::string &Name,
990 BasicBlock *InsertAtEnd)
991 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
992 assert(isValidOperands(V1, V2, Mask) &&
993 "Invalid shuffle vector instruction operands!");
994
995 Ops[0].init(V1, this);
996 Ops[1].init(V2, this);
997 Ops[2].init(Mask, this);
998}
999
1000bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1001 const Value *Mask) {
1002 if (!isa<PackedType>(V1->getType())) return false;
1003 if (V1->getType() != V2->getType()) return false;
1004 if (!isa<PackedType>(Mask->getType()) ||
Reid Spencer8d9336d2006-12-31 05:26:44 +00001005 cast<PackedType>(Mask->getType())->getElementType() != Type::Int32Ty ||
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001006 cast<PackedType>(Mask->getType())->getNumElements() !=
1007 cast<PackedType>(V1->getType())->getNumElements())
1008 return false;
1009 return true;
1010}
1011
1012
1013//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001014// BinaryOperator Class
1015//===----------------------------------------------------------------------===//
1016
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001017void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001018{
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001019 Value *LHS = getOperand(0), *RHS = getOperand(1);
1020 assert(LHS->getType() == RHS->getType() &&
1021 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001022#ifndef NDEBUG
1023 switch (iType) {
1024 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001025 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001026 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001027 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001028 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001029 isa<PackedType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001030 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001032 case UDiv:
1033 case SDiv:
1034 assert(getType() == LHS->getType() &&
1035 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001036 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1037 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001038 "Incorrect operand type (not integer) for S/UDIV");
1039 break;
1040 case FDiv:
1041 assert(getType() == LHS->getType() &&
1042 "Arithmetic operation should return same type as operands!");
1043 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1044 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1045 && "Incorrect operand type (not floating point) for FDIV");
1046 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001047 case URem:
1048 case SRem:
1049 assert(getType() == LHS->getType() &&
1050 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001051 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1052 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001053 "Incorrect operand type (not integer) for S/UREM");
1054 break;
1055 case FRem:
1056 assert(getType() == LHS->getType() &&
1057 "Arithmetic operation should return same type as operands!");
1058 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1059 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1060 && "Incorrect operand type (not floating point) for FREM");
1061 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062 case And: case Or:
1063 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001064 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001066 assert((getType()->isInteger() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001067 (isa<PackedType>(getType()) &&
Chris Lattner03c49532007-01-15 02:27:26 +00001068 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001069 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001071 default:
1072 break;
1073 }
1074#endif
1075}
1076
1077BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001078 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001079 Instruction *InsertBefore) {
1080 assert(S1->getType() == S2->getType() &&
1081 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001082 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083}
1084
1085BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001086 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001087 BasicBlock *InsertAtEnd) {
1088 BinaryOperator *Res = create(Op, S1, S2, Name);
1089 InsertAtEnd->getInstList().push_back(Res);
1090 return Res;
1091}
1092
1093BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1094 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001095 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1096 return new BinaryOperator(Instruction::Sub,
1097 zero, Op,
1098 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001099}
1100
1101BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1102 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001103 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1104 return new BinaryOperator(Instruction::Sub,
1105 zero, Op,
1106 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001107}
1108
1109BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1110 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001111 Constant *C;
1112 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001113 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001114 C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
1115 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001116 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001117 }
1118
1119 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001120 Op->getType(), Name, InsertBefore);
1121}
1122
1123BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1124 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001125 Constant *AllOnes;
1126 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1127 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001128 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001129 AllOnes =
1130 ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1131 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001132 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001133 }
1134
1135 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136 Op->getType(), Name, InsertAtEnd);
1137}
1138
1139
1140// isConstantAllOnes - Helper function for several functions below
1141static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001142 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143}
1144
1145bool BinaryOperator::isNeg(const Value *V) {
1146 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1147 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001148 return Bop->getOperand(0) ==
1149 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001150 return false;
1151}
1152
1153bool BinaryOperator::isNot(const Value *V) {
1154 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1155 return (Bop->getOpcode() == Instruction::Xor &&
1156 (isConstantAllOnes(Bop->getOperand(1)) ||
1157 isConstantAllOnes(Bop->getOperand(0))));
1158 return false;
1159}
1160
Chris Lattner2c7d1772005-04-24 07:28:37 +00001161Value *BinaryOperator::getNegArgument(Value *BinOp) {
1162 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1163 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001164}
1165
Chris Lattner2c7d1772005-04-24 07:28:37 +00001166const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1167 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001168}
1169
Chris Lattner2c7d1772005-04-24 07:28:37 +00001170Value *BinaryOperator::getNotArgument(Value *BinOp) {
1171 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1172 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1173 Value *Op0 = BO->getOperand(0);
1174 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001175 if (isConstantAllOnes(Op0)) return Op1;
1176
1177 assert(isConstantAllOnes(Op1));
1178 return Op0;
1179}
1180
Chris Lattner2c7d1772005-04-24 07:28:37 +00001181const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1182 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001183}
1184
1185
1186// swapOperands - Exchange the two operands to this instruction. This
1187// instruction is safe to use on any binary instruction and does not
1188// modify the semantics of the instruction. If the instruction is
1189// order dependent (SetLT f.e.) the opcode is changed.
1190//
1191bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001192 if (!isCommutative())
1193 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001194 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001195 return false;
1196}
1197
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001198//===----------------------------------------------------------------------===//
1199// CastInst Class
1200//===----------------------------------------------------------------------===//
1201
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001202// Just determine if this cast only deals with integral->integral conversion.
1203bool CastInst::isIntegerCast() const {
1204 switch (getOpcode()) {
1205 default: return false;
1206 case Instruction::ZExt:
1207 case Instruction::SExt:
1208 case Instruction::Trunc:
1209 return true;
1210 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001211 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001212 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001213}
1214
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001215bool CastInst::isLosslessCast() const {
1216 // Only BitCast can be lossless, exit fast if we're not BitCast
1217 if (getOpcode() != Instruction::BitCast)
1218 return false;
1219
1220 // Identity cast is always lossless
1221 const Type* SrcTy = getOperand(0)->getType();
1222 const Type* DstTy = getType();
1223 if (SrcTy == DstTy)
1224 return true;
1225
Reid Spencer8d9336d2006-12-31 05:26:44 +00001226 // Pointer to pointer is always lossless.
1227 if (isa<PointerType>(SrcTy))
1228 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001229 return false; // Other types have no identity values
1230}
1231
1232/// This function determines if the CastInst does not require any bits to be
1233/// changed in order to effect the cast. Essentially, it identifies cases where
1234/// no code gen is necessary for the cast, hence the name no-op cast. For
1235/// example, the following are all no-op casts:
1236/// # bitcast uint %X, int
1237/// # bitcast uint* %x, sbyte*
1238/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1239/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1240/// @brief Determine if a cast is a no-op.
1241bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1242 switch (getOpcode()) {
1243 default:
1244 assert(!"Invalid CastOp");
1245 case Instruction::Trunc:
1246 case Instruction::ZExt:
1247 case Instruction::SExt:
1248 case Instruction::FPTrunc:
1249 case Instruction::FPExt:
1250 case Instruction::UIToFP:
1251 case Instruction::SIToFP:
1252 case Instruction::FPToUI:
1253 case Instruction::FPToSI:
1254 return false; // These always modify bits
1255 case Instruction::BitCast:
1256 return true; // BitCast never modifies bits.
1257 case Instruction::PtrToInt:
1258 return IntPtrTy->getPrimitiveSizeInBits() ==
1259 getType()->getPrimitiveSizeInBits();
1260 case Instruction::IntToPtr:
1261 return IntPtrTy->getPrimitiveSizeInBits() ==
1262 getOperand(0)->getType()->getPrimitiveSizeInBits();
1263 }
1264}
1265
1266/// This function determines if a pair of casts can be eliminated and what
1267/// opcode should be used in the elimination. This assumes that there are two
1268/// instructions like this:
1269/// * %F = firstOpcode SrcTy %x to MidTy
1270/// * %S = secondOpcode MidTy %F to DstTy
1271/// The function returns a resultOpcode so these two casts can be replaced with:
1272/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1273/// If no such cast is permited, the function returns 0.
1274unsigned CastInst::isEliminableCastPair(
1275 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1276 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1277{
1278 // Define the 144 possibilities for these two cast instructions. The values
1279 // in this matrix determine what to do in a given situation and select the
1280 // case in the switch below. The rows correspond to firstOp, the columns
1281 // correspond to secondOp. In looking at the table below, keep in mind
1282 // the following cast properties:
1283 //
1284 // Size Compare Source Destination
1285 // Operator Src ? Size Type Sign Type Sign
1286 // -------- ------------ ------------------- ---------------------
1287 // TRUNC > Integer Any Integral Any
1288 // ZEXT < Integral Unsigned Integer Any
1289 // SEXT < Integral Signed Integer Any
1290 // FPTOUI n/a FloatPt n/a Integral Unsigned
1291 // FPTOSI n/a FloatPt n/a Integral Signed
1292 // UITOFP n/a Integral Unsigned FloatPt n/a
1293 // SITOFP n/a Integral Signed FloatPt n/a
1294 // FPTRUNC > FloatPt n/a FloatPt n/a
1295 // FPEXT < FloatPt n/a FloatPt n/a
1296 // PTRTOINT n/a Pointer n/a Integral Unsigned
1297 // INTTOPTR n/a Integral Unsigned Pointer n/a
1298 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001299 //
1300 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1301 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1302 // into "fptoui double to ulong", but this loses information about the range
1303 // of the produced value (we no longer know the top-part is all zeros).
1304 // Further this conversion is often much more expensive for typical hardware,
1305 // and causes issues when building libgcc. We disallow fptosi+sext for the
1306 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001307 const unsigned numCastOps =
1308 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1309 static const uint8_t CastResults[numCastOps][numCastOps] = {
1310 // T F F U S F F P I B -+
1311 // R Z S P P I I T P 2 N T |
1312 // U E E 2 2 2 2 R E I T C +- secondOp
1313 // N X X U S F F N X N 2 V |
1314 // C T T I I P P C T T P T -+
1315 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1316 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1317 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001318 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1319 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001320 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1321 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1322 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1323 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1324 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1325 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1326 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1327 };
1328
1329 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1330 [secondOp-Instruction::CastOpsBegin];
1331 switch (ElimCase) {
1332 case 0:
1333 // categorically disallowed
1334 return 0;
1335 case 1:
1336 // allowed, use first cast's opcode
1337 return firstOp;
1338 case 2:
1339 // allowed, use second cast's opcode
1340 return secondOp;
1341 case 3:
1342 // no-op cast in second op implies firstOp as long as the DestTy
1343 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001344 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001345 return firstOp;
1346 return 0;
1347 case 4:
1348 // no-op cast in second op implies firstOp as long as the DestTy
1349 // is floating point
1350 if (DstTy->isFloatingPoint())
1351 return firstOp;
1352 return 0;
1353 case 5:
1354 // no-op cast in first op implies secondOp as long as the SrcTy
1355 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001356 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001357 return secondOp;
1358 return 0;
1359 case 6:
1360 // no-op cast in first op implies secondOp as long as the SrcTy
1361 // is a floating point
1362 if (SrcTy->isFloatingPoint())
1363 return secondOp;
1364 return 0;
1365 case 7: {
1366 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1367 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1368 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1369 if (MidSize >= PtrSize)
1370 return Instruction::BitCast;
1371 return 0;
1372 }
1373 case 8: {
1374 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1375 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1376 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1377 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1378 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1379 if (SrcSize == DstSize)
1380 return Instruction::BitCast;
1381 else if (SrcSize < DstSize)
1382 return firstOp;
1383 return secondOp;
1384 }
1385 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1386 return Instruction::ZExt;
1387 case 10:
1388 // fpext followed by ftrunc is allowed if the bit size returned to is
1389 // the same as the original, in which case its just a bitcast
1390 if (SrcTy == DstTy)
1391 return Instruction::BitCast;
1392 return 0; // If the types are not the same we can't eliminate it.
1393 case 11:
1394 // bitcast followed by ptrtoint is allowed as long as the bitcast
1395 // is a pointer to pointer cast.
1396 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1397 return secondOp;
1398 return 0;
1399 case 12:
1400 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1401 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1402 return firstOp;
1403 return 0;
1404 case 13: {
1405 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1406 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1407 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1408 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1409 if (SrcSize <= PtrSize && SrcSize == DstSize)
1410 return Instruction::BitCast;
1411 return 0;
1412 }
1413 case 99:
1414 // cast combination can't happen (error in input). This is for all cases
1415 // where the MidTy is not the same for the two cast instructions.
1416 assert(!"Invalid Cast Combination");
1417 return 0;
1418 default:
1419 assert(!"Error in CastResults table!!!");
1420 return 0;
1421 }
1422 return 0;
1423}
1424
1425CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1426 const std::string &Name, Instruction *InsertBefore) {
1427 // Construct and return the appropriate CastInst subclass
1428 switch (op) {
1429 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1430 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1431 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1432 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1433 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1434 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1435 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1436 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1437 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1438 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1439 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1440 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1441 default:
1442 assert(!"Invalid opcode provided");
1443 }
1444 return 0;
1445}
1446
1447CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1448 const std::string &Name, BasicBlock *InsertAtEnd) {
1449 // Construct and return the appropriate CastInst subclass
1450 switch (op) {
1451 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1452 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1453 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1454 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1455 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1456 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1457 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1458 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1459 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1460 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1461 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1462 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1463 default:
1464 assert(!"Invalid opcode provided");
1465 }
1466 return 0;
1467}
1468
Reid Spencer5c140882006-12-04 20:17:56 +00001469CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1470 const std::string &Name,
1471 Instruction *InsertBefore) {
1472 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1473 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1474 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1475}
1476
1477CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1478 const std::string &Name,
1479 BasicBlock *InsertAtEnd) {
1480 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1481 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1482 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1483}
1484
1485CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1486 const std::string &Name,
1487 Instruction *InsertBefore) {
1488 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1489 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1490 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1491}
1492
1493CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1494 const std::string &Name,
1495 BasicBlock *InsertAtEnd) {
1496 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1497 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1498 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1499}
1500
1501CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1502 const std::string &Name,
1503 Instruction *InsertBefore) {
1504 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1505 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1506 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1507}
1508
1509CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1510 const std::string &Name,
1511 BasicBlock *InsertAtEnd) {
1512 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1513 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1514 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1515}
1516
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001517CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1518 const std::string &Name,
1519 BasicBlock *InsertAtEnd) {
1520 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001521 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001522 "Invalid cast");
1523
Chris Lattner03c49532007-01-15 02:27:26 +00001524 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001525 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1526 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1527}
1528
1529/// @brief Create a BitCast or a PtrToInt cast instruction
1530CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1531 const std::string &Name,
1532 Instruction *InsertBefore) {
1533 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001534 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001535 "Invalid cast");
1536
Chris Lattner03c49532007-01-15 02:27:26 +00001537 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001538 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1539 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1540}
1541
Reid Spencer7e933472006-12-12 00:49:44 +00001542CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1543 bool isSigned, const std::string &Name,
1544 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001545 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001546 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1547 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1548 Instruction::CastOps opcode =
1549 (SrcBits == DstBits ? Instruction::BitCast :
1550 (SrcBits > DstBits ? Instruction::Trunc :
1551 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1552 return create(opcode, C, Ty, Name, InsertBefore);
1553}
1554
1555CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1556 bool isSigned, const std::string &Name,
1557 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001558 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001559 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1560 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1561 Instruction::CastOps opcode =
1562 (SrcBits == DstBits ? Instruction::BitCast :
1563 (SrcBits > DstBits ? Instruction::Trunc :
1564 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1565 return create(opcode, C, Ty, Name, InsertAtEnd);
1566}
1567
1568CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1569 const std::string &Name,
1570 Instruction *InsertBefore) {
1571 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1572 "Invalid cast");
1573 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1574 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1575 Instruction::CastOps opcode =
1576 (SrcBits == DstBits ? Instruction::BitCast :
1577 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1578 return create(opcode, C, Ty, Name, InsertBefore);
1579}
1580
1581CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1582 const std::string &Name,
1583 BasicBlock *InsertAtEnd) {
1584 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1585 "Invalid cast");
1586 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1587 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1588 Instruction::CastOps opcode =
1589 (SrcBits == DstBits ? Instruction::BitCast :
1590 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1591 return create(opcode, C, Ty, Name, InsertAtEnd);
1592}
1593
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001594// Provide a way to get a "cast" where the cast opcode is inferred from the
1595// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001596// logic in the castIsValid function below. This axiom should hold:
1597// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1598// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001599// casting opcode for the arguments passed to it.
1600Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001601CastInst::getCastOpcode(
1602 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001603 // Get the bit sizes, we'll need these
1604 const Type *SrcTy = Src->getType();
1605 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1606 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1607
1608 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001609 if (DestTy->isInteger()) { // Casting to integral
1610 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001611 if (DestBits < SrcBits)
1612 return Trunc; // int -> smaller int
1613 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001614 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001615 return SExt; // signed -> SEXT
1616 else
1617 return ZExt; // unsigned -> ZEXT
1618 } else {
1619 return BitCast; // Same size, No-op cast
1620 }
1621 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001622 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001623 return FPToSI; // FP -> sint
1624 else
1625 return FPToUI; // FP -> uint
1626 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1627 assert(DestBits == PTy->getBitWidth() &&
1628 "Casting packed to integer of different width");
1629 return BitCast; // Same size, no-op cast
1630 } else {
1631 assert(isa<PointerType>(SrcTy) &&
1632 "Casting from a value that is not first-class type");
1633 return PtrToInt; // ptr -> int
1634 }
1635 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001636 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001637 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001638 return SIToFP; // sint -> FP
1639 else
1640 return UIToFP; // uint -> FP
1641 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1642 if (DestBits < SrcBits) {
1643 return FPTrunc; // FP -> smaller FP
1644 } else if (DestBits > SrcBits) {
1645 return FPExt; // FP -> larger FP
1646 } else {
1647 return BitCast; // same size, no-op cast
1648 }
1649 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1650 assert(DestBits == PTy->getBitWidth() &&
1651 "Casting packed to floating point of different width");
1652 return BitCast; // same size, no-op cast
1653 } else {
1654 assert(0 && "Casting pointer or non-first class to float");
1655 }
1656 } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
1657 if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) {
1658 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1659 "Casting packed to packed of different widths");
1660 return BitCast; // packed -> packed
1661 } else if (DestPTy->getBitWidth() == SrcBits) {
1662 return BitCast; // float/int -> packed
1663 } else {
1664 assert(!"Illegal cast to packed (wrong type or size)");
1665 }
1666 } else if (isa<PointerType>(DestTy)) {
1667 if (isa<PointerType>(SrcTy)) {
1668 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001669 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001670 return IntToPtr; // int -> ptr
1671 } else {
1672 assert(!"Casting pointer to other than pointer or int");
1673 }
1674 } else {
1675 assert(!"Casting to type that is not first-class");
1676 }
1677
1678 // If we fall through to here we probably hit an assertion cast above
1679 // and assertions are not turned on. Anything we return is an error, so
1680 // BitCast is as good a choice as any.
1681 return BitCast;
1682}
1683
1684//===----------------------------------------------------------------------===//
1685// CastInst SubClass Constructors
1686//===----------------------------------------------------------------------===//
1687
1688/// Check that the construction parameters for a CastInst are correct. This
1689/// could be broken out into the separate constructors but it is useful to have
1690/// it in one place and to eliminate the redundant code for getting the sizes
1691/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001692bool
1693CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001694
1695 // Check for type sanity on the arguments
1696 const Type *SrcTy = S->getType();
1697 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1698 return false;
1699
1700 // Get the size of the types in bits, we'll need this later
1701 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1702 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1703
1704 // Switch on the opcode provided
1705 switch (op) {
1706 default: return false; // This is an input error
1707 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001708 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001709 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001710 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001711 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001712 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001713 case Instruction::FPTrunc:
1714 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1715 SrcBitSize > DstBitSize;
1716 case Instruction::FPExt:
1717 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1718 SrcBitSize < DstBitSize;
1719 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001720 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001721 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001722 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001723 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001724 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001725 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001726 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001727 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001728 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001729 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001730 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001731 case Instruction::BitCast:
1732 // BitCast implies a no-op cast of type only. No bits change.
1733 // However, you can't cast pointers to anything but pointers.
1734 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1735 return false;
1736
1737 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1738 // these cases, the cast is okay if the source and destination bit widths
1739 // are identical.
1740 return SrcBitSize == DstBitSize;
1741 }
1742}
1743
1744TruncInst::TruncInst(
1745 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1746) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001747 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001748}
1749
1750TruncInst::TruncInst(
1751 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1752) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001753 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001754}
1755
1756ZExtInst::ZExtInst(
1757 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1758) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001759 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001760}
1761
1762ZExtInst::ZExtInst(
1763 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1764) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001765 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001766}
1767SExtInst::SExtInst(
1768 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1769) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001770 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001771}
1772
Jeff Cohencc08c832006-12-02 02:22:01 +00001773SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001774 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1775) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001776 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001777}
1778
1779FPTruncInst::FPTruncInst(
1780 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1781) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001782 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001783}
1784
1785FPTruncInst::FPTruncInst(
1786 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1787) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001788 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789}
1790
1791FPExtInst::FPExtInst(
1792 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1793) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001794 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795}
1796
1797FPExtInst::FPExtInst(
1798 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1799) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001800 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001801}
1802
1803UIToFPInst::UIToFPInst(
1804 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1805) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001806 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001807}
1808
1809UIToFPInst::UIToFPInst(
1810 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1811) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001812 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813}
1814
1815SIToFPInst::SIToFPInst(
1816 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1817) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001818 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001819}
1820
1821SIToFPInst::SIToFPInst(
1822 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1823) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001824 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001825}
1826
1827FPToUIInst::FPToUIInst(
1828 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1829) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001830 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001831}
1832
1833FPToUIInst::FPToUIInst(
1834 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1835) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001836 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001837}
1838
1839FPToSIInst::FPToSIInst(
1840 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1841) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001842 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001843}
1844
1845FPToSIInst::FPToSIInst(
1846 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1847) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001848 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001849}
1850
1851PtrToIntInst::PtrToIntInst(
1852 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1853) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001854 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001855}
1856
1857PtrToIntInst::PtrToIntInst(
1858 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1859) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001860 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001861}
1862
1863IntToPtrInst::IntToPtrInst(
1864 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1865) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001866 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001867}
1868
1869IntToPtrInst::IntToPtrInst(
1870 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1871) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001872 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001873}
1874
1875BitCastInst::BitCastInst(
1876 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1877) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001878 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879}
1880
1881BitCastInst::BitCastInst(
1882 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1883) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001884 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001885}
Chris Lattnerf16dc002006-09-17 19:29:56 +00001886
1887//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00001888// CmpInst Classes
1889//===----------------------------------------------------------------------===//
1890
1891CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1892 const std::string &Name, Instruction *InsertBefore)
Reid Spencer542964f2007-01-11 18:21:29 +00001893 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001894 Ops[0].init(LHS, this);
1895 Ops[1].init(RHS, this);
1896 SubclassData = predicate;
1897 if (op == Instruction::ICmp) {
1898 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1899 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1900 "Invalid ICmp predicate value");
1901 const Type* Op0Ty = getOperand(0)->getType();
1902 const Type* Op1Ty = getOperand(1)->getType();
1903 assert(Op0Ty == Op1Ty &&
1904 "Both operands to ICmp instruction are not of the same type!");
1905 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001906 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001907 "Invalid operand types for ICmp instruction");
1908 return;
1909 }
1910 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1911 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1912 "Invalid FCmp predicate value");
1913 const Type* Op0Ty = getOperand(0)->getType();
1914 const Type* Op1Ty = getOperand(1)->getType();
1915 assert(Op0Ty == Op1Ty &&
1916 "Both operands to FCmp instruction are not of the same type!");
1917 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001918 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001919 "Invalid operand types for FCmp instruction");
1920}
1921
1922CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1923 const std::string &Name, BasicBlock *InsertAtEnd)
Reid Spencer542964f2007-01-11 18:21:29 +00001924 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001925 Ops[0].init(LHS, this);
1926 Ops[1].init(RHS, this);
1927 SubclassData = predicate;
1928 if (op == Instruction::ICmp) {
1929 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1930 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1931 "Invalid ICmp predicate value");
1932
1933 const Type* Op0Ty = getOperand(0)->getType();
1934 const Type* Op1Ty = getOperand(1)->getType();
1935 assert(Op0Ty == Op1Ty &&
1936 "Both operands to ICmp instruction are not of the same type!");
1937 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001938 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001939 "Invalid operand types for ICmp instruction");
1940 return;
1941 }
1942 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1943 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1944 "Invalid FCmp predicate value");
1945 const Type* Op0Ty = getOperand(0)->getType();
1946 const Type* Op1Ty = getOperand(1)->getType();
1947 assert(Op0Ty == Op1Ty &&
1948 "Both operands to FCmp instruction are not of the same type!");
1949 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001950 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001951 "Invalid operand types for FCmp instruction");
1952}
1953
1954CmpInst *
1955CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1956 const std::string &Name, Instruction *InsertBefore) {
1957 if (Op == Instruction::ICmp) {
1958 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1959 InsertBefore);
1960 }
1961 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1962 InsertBefore);
1963}
1964
1965CmpInst *
1966CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1967 const std::string &Name, BasicBlock *InsertAtEnd) {
1968 if (Op == Instruction::ICmp) {
1969 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1970 InsertAtEnd);
1971 }
1972 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1973 InsertAtEnd);
1974}
1975
1976void CmpInst::swapOperands() {
1977 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
1978 IC->swapOperands();
1979 else
1980 cast<FCmpInst>(this)->swapOperands();
1981}
1982
1983bool CmpInst::isCommutative() {
1984 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
1985 return IC->isCommutative();
1986 return cast<FCmpInst>(this)->isCommutative();
1987}
1988
1989bool CmpInst::isEquality() {
1990 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
1991 return IC->isEquality();
1992 return cast<FCmpInst>(this)->isEquality();
1993}
1994
1995
1996ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
1997 switch (pred) {
1998 default:
1999 assert(!"Unknown icmp predicate!");
2000 case ICMP_EQ: return ICMP_NE;
2001 case ICMP_NE: return ICMP_EQ;
2002 case ICMP_UGT: return ICMP_ULE;
2003 case ICMP_ULT: return ICMP_UGE;
2004 case ICMP_UGE: return ICMP_ULT;
2005 case ICMP_ULE: return ICMP_UGT;
2006 case ICMP_SGT: return ICMP_SLE;
2007 case ICMP_SLT: return ICMP_SGE;
2008 case ICMP_SGE: return ICMP_SLT;
2009 case ICMP_SLE: return ICMP_SGT;
2010 }
2011}
2012
2013ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2014 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002015 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002016 case ICMP_EQ: case ICMP_NE:
2017 return pred;
2018 case ICMP_SGT: return ICMP_SLT;
2019 case ICMP_SLT: return ICMP_SGT;
2020 case ICMP_SGE: return ICMP_SLE;
2021 case ICMP_SLE: return ICMP_SGE;
2022 case ICMP_UGT: return ICMP_ULT;
2023 case ICMP_ULT: return ICMP_UGT;
2024 case ICMP_UGE: return ICMP_ULE;
2025 case ICMP_ULE: return ICMP_UGE;
2026 }
2027}
2028
Reid Spencer266e42b2006-12-23 06:05:41 +00002029ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2030 switch (pred) {
2031 default: assert(! "Unknown icmp predicate!");
2032 case ICMP_EQ: case ICMP_NE:
2033 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2034 return pred;
2035 case ICMP_UGT: return ICMP_SGT;
2036 case ICMP_ULT: return ICMP_SLT;
2037 case ICMP_UGE: return ICMP_SGE;
2038 case ICMP_ULE: return ICMP_SLE;
2039 }
2040}
2041
2042bool ICmpInst::isSignedPredicate(Predicate pred) {
2043 switch (pred) {
2044 default: assert(! "Unknown icmp predicate!");
2045 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2046 return true;
2047 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2048 case ICMP_UGE: case ICMP_ULE:
2049 return false;
2050 }
2051}
2052
Reid Spencerd9436b62006-11-20 01:22:35 +00002053FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2054 switch (pred) {
2055 default:
2056 assert(!"Unknown icmp predicate!");
2057 case FCMP_OEQ: return FCMP_UNE;
2058 case FCMP_ONE: return FCMP_UEQ;
2059 case FCMP_OGT: return FCMP_ULE;
2060 case FCMP_OLT: return FCMP_UGE;
2061 case FCMP_OGE: return FCMP_ULT;
2062 case FCMP_OLE: return FCMP_UGT;
2063 case FCMP_UEQ: return FCMP_ONE;
2064 case FCMP_UNE: return FCMP_OEQ;
2065 case FCMP_UGT: return FCMP_OLE;
2066 case FCMP_ULT: return FCMP_OGE;
2067 case FCMP_UGE: return FCMP_OLT;
2068 case FCMP_ULE: return FCMP_OGT;
2069 case FCMP_ORD: return FCMP_UNO;
2070 case FCMP_UNO: return FCMP_ORD;
2071 case FCMP_TRUE: return FCMP_FALSE;
2072 case FCMP_FALSE: return FCMP_TRUE;
2073 }
2074}
2075
2076FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2077 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002078 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002079 case FCMP_FALSE: case FCMP_TRUE:
2080 case FCMP_OEQ: case FCMP_ONE:
2081 case FCMP_UEQ: case FCMP_UNE:
2082 case FCMP_ORD: case FCMP_UNO:
2083 return pred;
2084 case FCMP_OGT: return FCMP_OLT;
2085 case FCMP_OLT: return FCMP_OGT;
2086 case FCMP_OGE: return FCMP_OLE;
2087 case FCMP_OLE: return FCMP_OGE;
2088 case FCMP_UGT: return FCMP_ULT;
2089 case FCMP_ULT: return FCMP_UGT;
2090 case FCMP_UGE: return FCMP_ULE;
2091 case FCMP_ULE: return FCMP_UGE;
2092 }
2093}
2094
Reid Spencer266e42b2006-12-23 06:05:41 +00002095bool CmpInst::isUnsigned(unsigned short predicate) {
2096 switch (predicate) {
2097 default: return false;
2098 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2099 case ICmpInst::ICMP_UGE: return true;
2100 }
2101}
2102
2103bool CmpInst::isSigned(unsigned short predicate){
2104 switch (predicate) {
2105 default: return false;
2106 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2107 case ICmpInst::ICMP_SGE: return true;
2108 }
2109}
2110
2111bool CmpInst::isOrdered(unsigned short predicate) {
2112 switch (predicate) {
2113 default: return false;
2114 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2115 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2116 case FCmpInst::FCMP_ORD: return true;
2117 }
2118}
2119
2120bool CmpInst::isUnordered(unsigned short predicate) {
2121 switch (predicate) {
2122 default: return false;
2123 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2124 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2125 case FCmpInst::FCMP_UNO: return true;
2126 }
2127}
2128
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002129//===----------------------------------------------------------------------===//
2130// SwitchInst Implementation
2131//===----------------------------------------------------------------------===//
2132
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002133void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002134 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002135 ReservedSpace = 2+NumCases*2;
2136 NumOperands = 2;
2137 OperandList = new Use[ReservedSpace];
2138
2139 OperandList[0].init(Value, this);
2140 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002141}
2142
Misha Brukmanb1c93172005-04-21 23:48:37 +00002143SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002144 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
2145 SI.getNumOperands()) {
2146 Use *OL = OperandList, *InOL = SI.OperandList;
2147 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2148 OL[i].init(InOL[i], this);
2149 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002150 }
2151}
2152
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002153SwitchInst::~SwitchInst() {
2154 delete [] OperandList;
2155}
2156
2157
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002158/// addCase - Add an entry to the switch instruction...
2159///
Chris Lattner47ac1872005-02-24 05:32:09 +00002160void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002161 unsigned OpNo = NumOperands;
2162 if (OpNo+2 > ReservedSpace)
2163 resizeOperands(0); // Get more space!
2164 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002165 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002166 NumOperands = OpNo+2;
2167 OperandList[OpNo].init(OnVal, this);
2168 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169}
2170
2171/// removeCase - This method removes the specified successor from the switch
2172/// instruction. Note that this cannot be used to remove the default
2173/// destination (successor #0).
2174///
2175void SwitchInst::removeCase(unsigned idx) {
2176 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002177 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2178
2179 unsigned NumOps = getNumOperands();
2180 Use *OL = OperandList;
2181
2182 // Move everything after this operand down.
2183 //
2184 // FIXME: we could just swap with the end of the list, then erase. However,
2185 // client might not expect this to happen. The code as it is thrashes the
2186 // use/def lists, which is kinda lame.
2187 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2188 OL[i-2] = OL[i];
2189 OL[i-2+1] = OL[i+1];
2190 }
2191
2192 // Nuke the last value.
2193 OL[NumOps-2].set(0);
2194 OL[NumOps-2+1].set(0);
2195 NumOperands = NumOps-2;
2196}
2197
2198/// resizeOperands - resize operands - This adjusts the length of the operands
2199/// list according to the following behavior:
2200/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2201/// of operation. This grows the number of ops by 1.5 times.
2202/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2203/// 3. If NumOps == NumOperands, trim the reserved space.
2204///
2205void SwitchInst::resizeOperands(unsigned NumOps) {
2206 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002207 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002208 } else if (NumOps*2 > NumOperands) {
2209 // No resize needed.
2210 if (ReservedSpace >= NumOps) return;
2211 } else if (NumOps == NumOperands) {
2212 if (ReservedSpace == NumOps) return;
2213 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002214 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002215 }
2216
2217 ReservedSpace = NumOps;
2218 Use *NewOps = new Use[NumOps];
2219 Use *OldOps = OperandList;
2220 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2221 NewOps[i].init(OldOps[i], this);
2222 OldOps[i].set(0);
2223 }
2224 delete [] OldOps;
2225 OperandList = NewOps;
2226}
2227
2228
2229BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2230 return getSuccessor(idx);
2231}
2232unsigned SwitchInst::getNumSuccessorsV() const {
2233 return getNumSuccessors();
2234}
2235void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2236 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002237}
Chris Lattnerf22be932004-10-15 23:52:53 +00002238
2239
2240// Define these methods here so vtables don't get emitted into every translation
2241// unit that uses these classes.
2242
2243GetElementPtrInst *GetElementPtrInst::clone() const {
2244 return new GetElementPtrInst(*this);
2245}
2246
2247BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002248 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002249}
2250
Reid Spencerd9436b62006-11-20 01:22:35 +00002251CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002252 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002253}
2254
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2256AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2257FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2258LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2259StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2260CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2261CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2262CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2263CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2264CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2265CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2266CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2267CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2268CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2269CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2270CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2271CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2272CallInst *CallInst::clone() const { return new CallInst(*this); }
2273ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
2274SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2275VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2276
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002277ExtractElementInst *ExtractElementInst::clone() const {
2278 return new ExtractElementInst(*this);
2279}
2280InsertElementInst *InsertElementInst::clone() const {
2281 return new InsertElementInst(*this);
2282}
2283ShuffleVectorInst *ShuffleVectorInst::clone() const {
2284 return new ShuffleVectorInst(*this);
2285}
Chris Lattnerf22be932004-10-15 23:52:53 +00002286PHINode *PHINode::clone() const { return new PHINode(*this); }
2287ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2288BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2289SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2290InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2291UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002292UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}