blob: c09ec3ce217151cca29ff32400a3a6b9f1b51214 [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"
Reid Spencer0286bc12007-02-28 22:00:54 +000021#include "llvm/Support/ConstantRange.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattnerf7b6d312005-05-06 20:26:43 +000024unsigned CallSite::getCallingConv() const {
25 if (CallInst *CI = dyn_cast<CallInst>(I))
26 return CI->getCallingConv();
27 else
28 return cast<InvokeInst>(I)->getCallingConv();
29}
30void CallSite::setCallingConv(unsigned CC) {
31 if (CallInst *CI = dyn_cast<CallInst>(I))
32 CI->setCallingConv(CC);
33 else
34 cast<InvokeInst>(I)->setCallingConv(CC);
35}
36
37
Chris Lattner1c12a882006-06-21 16:53:47 +000038
39
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000040//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000041// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
Chris Lattner1c12a882006-06-21 16:53:47 +000044// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
48// Out of line virtual method, so the vtable, etc has a home.
49UnaryInstruction::~UnaryInstruction() {
50}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000051
52
53//===----------------------------------------------------------------------===//
54// PHINode Class
55//===----------------------------------------------------------------------===//
56
57PHINode::PHINode(const PHINode &PN)
58 : Instruction(PN.getType(), Instruction::PHI,
59 new Use[PN.getNumOperands()], PN.getNumOperands()),
60 ReservedSpace(PN.getNumOperands()) {
61 Use *OL = OperandList;
62 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
63 OL[i].init(PN.getOperand(i), this);
64 OL[i+1].init(PN.getOperand(i+1), this);
65 }
66}
67
68PHINode::~PHINode() {
69 delete [] OperandList;
70}
71
72// removeIncomingValue - Remove an incoming value. This is useful if a
73// predecessor basic block is deleted.
74Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
75 unsigned NumOps = getNumOperands();
76 Use *OL = OperandList;
77 assert(Idx*2 < NumOps && "BB not in PHI node!");
78 Value *Removed = OL[Idx*2];
79
80 // Move everything after this operand down.
81 //
82 // FIXME: we could just swap with the end of the list, then erase. However,
83 // client might not expect this to happen. The code as it is thrashes the
84 // use/def lists, which is kinda lame.
85 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
86 OL[i-2] = OL[i];
87 OL[i-2+1] = OL[i+1];
88 }
89
90 // Nuke the last value.
91 OL[NumOps-2].set(0);
92 OL[NumOps-2+1].set(0);
93 NumOperands = NumOps-2;
94
95 // If the PHI node is dead, because it has zero entries, nuke it now.
96 if (NumOps == 2 && DeletePHIIfEmpty) {
97 // If anyone is using this PHI, make them use a dummy value instead...
98 replaceAllUsesWith(UndefValue::get(getType()));
99 eraseFromParent();
100 }
101 return Removed;
102}
103
104/// resizeOperands - resize operands - This adjusts the length of the operands
105/// list according to the following behavior:
106/// 1. If NumOps == 0, grow the operand list in response to a push_back style
107/// of operation. This grows the number of ops by 1.5 times.
108/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
109/// 3. If NumOps == NumOperands, trim the reserved space.
110///
111void PHINode::resizeOperands(unsigned NumOps) {
112 if (NumOps == 0) {
113 NumOps = (getNumOperands())*3/2;
114 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
115 } else if (NumOps*2 > NumOperands) {
116 // No resize needed.
117 if (ReservedSpace >= NumOps) return;
118 } else if (NumOps == NumOperands) {
119 if (ReservedSpace == NumOps) return;
120 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000121 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 }
123
124 ReservedSpace = NumOps;
125 Use *NewOps = new Use[NumOps];
126 Use *OldOps = OperandList;
127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
128 NewOps[i].init(OldOps[i], this);
129 OldOps[i].set(0);
130 }
131 delete [] OldOps;
132 OperandList = NewOps;
133}
134
Nate Begemanb3923212005-08-04 23:24:19 +0000135/// hasConstantValue - If the specified PHI node always merges together the same
136/// value, return the value, otherwise return null.
137///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000138Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000139 // If the PHI node only has one incoming value, eliminate the PHI node...
140 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000141 if (getIncomingValue(0) != this) // not X = phi X
142 return getIncomingValue(0);
143 else
144 return UndefValue::get(getType()); // Self cycle is dead.
145
Nate Begemanb3923212005-08-04 23:24:19 +0000146 // Otherwise if all of the incoming values are the same for the PHI, replace
147 // the PHI node with the incoming value.
148 //
149 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000150 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000151 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000152 if (isa<UndefValue>(getIncomingValue(i)))
153 HasUndefInput = true;
154 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000155 if (InVal && getIncomingValue(i) != InVal)
156 return 0; // Not the same, bail out.
157 else
158 InVal = getIncomingValue(i);
159
160 // The only case that could cause InVal to be null is if we have a PHI node
161 // that only has entries for itself. In this case, there is no entry into the
162 // loop, so kill the PHI.
163 //
164 if (InVal == 0) InVal = UndefValue::get(getType());
165
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000166 // If we have a PHI node like phi(X, undef, X), where X is defined by some
167 // instruction, we cannot always return X as the result of the PHI node. Only
168 // do this if X is not an instruction (thus it must dominate the PHI block),
169 // or if the client is prepared to deal with this possibility.
170 if (HasUndefInput && !AllowNonDominatingInstruction)
171 if (Instruction *IV = dyn_cast<Instruction>(InVal))
172 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000173 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000174 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 return 0; // Cannot guarantee that InVal dominates this PHINode.
176
Nate Begemanb3923212005-08-04 23:24:19 +0000177 // All of the incoming values are the same, return the value now.
178 return InVal;
179}
180
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181
182//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000183// CallInst Implementation
184//===----------------------------------------------------------------------===//
185
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000186CallInst::~CallInst() {
187 delete [] OperandList;
188}
189
Chris Lattner054ba2c2007-02-13 00:58:44 +0000190void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000191 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000192 NumOperands = NumParams+1;
193 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000194 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000195
Misha Brukmanb1c93172005-04-21 23:48:37 +0000196 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000197 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000198 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199
Chris Lattner054ba2c2007-02-13 00:58:44 +0000200 assert((NumParams == FTy->getNumParams() ||
201 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000202 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000203 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000204 assert((i >= FTy->getNumParams() ||
205 FTy->getParamType(i) == Params[i]->getType()) &&
206 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000207 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000208 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000209}
210
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000212 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000213 NumOperands = 3;
214 Use *OL = OperandList = new Use[3];
215 OL[0].init(Func, this);
216 OL[1].init(Actual1, this);
217 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000218
219 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000220 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000221 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000222
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000224 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000225 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000226 assert((0 >= FTy->getNumParams() ||
227 FTy->getParamType(0) == Actual1->getType()) &&
228 "Calling a function with a bad signature!");
229 assert((1 >= FTy->getNumParams() ||
230 FTy->getParamType(1) == Actual2->getType()) &&
231 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232}
233
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000234void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000235 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000236 NumOperands = 2;
237 Use *OL = OperandList = new Use[2];
238 OL[0].init(Func, this);
239 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000240
241 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000243 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000245 assert((FTy->getNumParams() == 1 ||
246 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000248 assert((0 == FTy->getNumParams() ||
249 FTy->getParamType(0) == Actual->getType()) &&
250 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251}
252
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000254 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000255 NumOperands = 1;
256 Use *OL = OperandList = new Use[1];
257 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000259 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264}
265
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000266CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000267 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
269 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000270 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000271 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000272 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000273}
274CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
275 const std::string &Name, Instruction *InsertBefore)
276: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
277 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000278 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000279 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000280 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000281}
282
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283CallInst::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 Lattner2195fc42007-02-24 00:55:48 +0000287 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000289 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290}
291
292CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
293 const std::string &Name, BasicBlock *InsertAtEnd)
294 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
295 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000296 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000298 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
301CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000302 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
304 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000305 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000307 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308}
309
310CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
311 BasicBlock *InsertAtEnd)
312 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
313 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000314 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000316 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
318
319CallInst::CallInst(Value *Func, const std::string &Name,
320 Instruction *InsertBefore)
321 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
322 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000323 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000325 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326}
327
328CallInst::CallInst(Value *Func, const std::string &Name,
329 BasicBlock *InsertAtEnd)
330 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
331 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000332 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
336
Misha Brukmanb1c93172005-04-21 23:48:37 +0000337CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
339 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000340 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000341 Use *OL = OperandList;
342 Use *InOL = CI.OperandList;
343 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
344 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345}
346
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347
348//===----------------------------------------------------------------------===//
349// InvokeInst Implementation
350//===----------------------------------------------------------------------===//
351
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000352InvokeInst::~InvokeInst() {
353 delete [] OperandList;
354}
355
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000357 Value* const *Args, unsigned NumArgs) {
358 NumOperands = 3+NumArgs;
359 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000360 OL[0].init(Fn, this);
361 OL[1].init(IfNormal, this);
362 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000365 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000366
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000367 assert((NumArgs == FTy->getNumParams()) ||
368 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000370
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000371 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000372 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000373 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000374 "Invoking a function with a bad signature!");
375
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000376 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000377 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378}
379
380InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
381 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000382 Value* const *Args, unsigned NumArgs,
383 const std::string &Name, Instruction *InsertBefore)
384 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
385 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000386 Instruction::Invoke, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000387 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000388 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000389}
390
391InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
392 BasicBlock *IfException,
393 Value* const *Args, unsigned NumArgs,
394 const std::string &Name, BasicBlock *InsertAtEnd)
395 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
396 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000397 Instruction::Invoke, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000398 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000399 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000400}
401
Misha Brukmanb1c93172005-04-21 23:48:37 +0000402InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000403 : TerminatorInst(II.getType(), Instruction::Invoke,
404 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000405 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000406 Use *OL = OperandList, *InOL = II.OperandList;
407 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
408 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409}
410
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000411BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
412 return getSuccessor(idx);
413}
414unsigned InvokeInst::getNumSuccessorsV() const {
415 return getNumSuccessors();
416}
417void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
418 return setSuccessor(idx, B);
419}
420
421
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422//===----------------------------------------------------------------------===//
423// ReturnInst Implementation
424//===----------------------------------------------------------------------===//
425
Chris Lattner2195fc42007-02-24 00:55:48 +0000426ReturnInst::ReturnInst(const ReturnInst &RI)
427 : TerminatorInst(Type::VoidTy, Instruction::Ret,
428 &RetVal, RI.getNumOperands()) {
429 if (RI.getNumOperands())
430 RetVal.init(RI.RetVal, this);
431}
432
433ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
434 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
435 init(retVal);
436}
437ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
438 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
439 init(retVal);
440}
441ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
442 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
443}
444
445
446
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000447void ReturnInst::init(Value *retVal) {
448 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000449 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000450 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000451 NumOperands = 1;
452 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000453 }
454}
455
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000456unsigned ReturnInst::getNumSuccessorsV() const {
457 return getNumSuccessors();
458}
459
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000460// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
461// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000463 assert(0 && "ReturnInst has no successors!");
464}
465
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
467 assert(0 && "ReturnInst has no successors!");
468 abort();
469 return 0;
470}
471
472
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000473//===----------------------------------------------------------------------===//
474// UnwindInst Implementation
475//===----------------------------------------------------------------------===//
476
Chris Lattner2195fc42007-02-24 00:55:48 +0000477UnwindInst::UnwindInst(Instruction *InsertBefore)
478 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
479}
480UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
481 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
482}
483
484
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000485unsigned UnwindInst::getNumSuccessorsV() const {
486 return getNumSuccessors();
487}
488
489void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000490 assert(0 && "UnwindInst has no successors!");
491}
492
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000493BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
494 assert(0 && "UnwindInst has no successors!");
495 abort();
496 return 0;
497}
498
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000499//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000500// UnreachableInst Implementation
501//===----------------------------------------------------------------------===//
502
Chris Lattner2195fc42007-02-24 00:55:48 +0000503UnreachableInst::UnreachableInst(Instruction *InsertBefore)
504 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
505}
506UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
507 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
508}
509
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000510unsigned UnreachableInst::getNumSuccessorsV() const {
511 return getNumSuccessors();
512}
513
514void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
515 assert(0 && "UnwindInst has no successors!");
516}
517
518BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
519 assert(0 && "UnwindInst has no successors!");
520 abort();
521 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000522}
523
524//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525// BranchInst Implementation
526//===----------------------------------------------------------------------===//
527
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000528void BranchInst::AssertOK() {
529 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000530 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000531 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000532}
533
Chris Lattner2195fc42007-02-24 00:55:48 +0000534BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
535 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
536 assert(IfTrue != 0 && "Branch destination may not be null!");
537 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
538}
539BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
540 Instruction *InsertBefore)
541: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
542 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
543 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
544 Ops[2].init(Cond, this);
545#ifndef NDEBUG
546 AssertOK();
547#endif
548}
549
550BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
551 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
552 assert(IfTrue != 0 && "Branch destination may not be null!");
553 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
554}
555
556BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
557 BasicBlock *InsertAtEnd)
558 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
559 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
560 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
561 Ops[2].init(Cond, this);
562#ifndef NDEBUG
563 AssertOK();
564#endif
565}
566
567
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000569 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570 OperandList[0].init(BI.getOperand(0), this);
571 if (BI.getNumOperands() != 1) {
572 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
573 OperandList[1].init(BI.getOperand(1), this);
574 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575 }
576}
577
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
579 return getSuccessor(idx);
580}
581unsigned BranchInst::getNumSuccessorsV() const {
582 return getNumSuccessors();
583}
584void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
585 setSuccessor(idx, B);
586}
587
588
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589//===----------------------------------------------------------------------===//
590// AllocationInst Implementation
591//===----------------------------------------------------------------------===//
592
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000593static Value *getAISize(Value *Amt) {
594 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000595 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000596 else {
597 assert(!isa<BasicBlock>(Amt) &&
598 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000599 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000600 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000601 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000602 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603}
604
Misha Brukmanb1c93172005-04-21 23:48:37 +0000605AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000606 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000609 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000610 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000612 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613}
614
Misha Brukmanb1c93172005-04-21 23:48:37 +0000615AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000616 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000619 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000620 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000621 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000622 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623}
624
Chris Lattner1c12a882006-06-21 16:53:47 +0000625// Out of line virtual method, so the vtable, etc has a home.
626AllocationInst::~AllocationInst() {
627}
628
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000630 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
631 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633}
634
635const Type *AllocationInst::getAllocatedType() const {
636 return getType()->getElementType();
637}
638
639AllocaInst::AllocaInst(const AllocaInst &AI)
640 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000641 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000642}
643
644MallocInst::MallocInst(const MallocInst &MI)
645 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000646 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000647}
648
649//===----------------------------------------------------------------------===//
650// FreeInst Implementation
651//===----------------------------------------------------------------------===//
652
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653void FreeInst::AssertOK() {
654 assert(isa<PointerType>(getOperand(0)->getType()) &&
655 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656}
657
658FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000659 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661}
662
663FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000664 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000666}
667
668
669//===----------------------------------------------------------------------===//
670// LoadInst Implementation
671//===----------------------------------------------------------------------===//
672
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000674 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676}
677
678LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000680 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000681 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000682 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000683 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684}
685
686LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000688 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000689 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000690 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000691 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692}
693
694LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
695 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000697 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000698 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000700 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701}
702
703LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
704 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000706 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000707 setVolatile(isVolatile);
708 AssertOK();
709 setName(Name);
710}
711
712
713
714LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000715 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
716 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000717 setVolatile(false);
718 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000719 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000720}
721
722LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000723 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
724 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000725 setVolatile(false);
726 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000727 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000728}
729
730LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
731 Instruction *InsertBef)
732: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000734 setVolatile(isVolatile);
735 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000736 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000737}
738
739LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
740 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000741 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
742 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000743 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000745 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
748
749//===----------------------------------------------------------------------===//
750// StoreInst Implementation
751//===----------------------------------------------------------------------===//
752
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753void StoreInst::AssertOK() {
754 assert(isa<PointerType>(getOperand(1)->getType()) &&
755 "Ptr must have pointer type!");
756 assert(getOperand(0)->getType() ==
757 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000758 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000759}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000761
762StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000763 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764 Ops[0].init(val, this);
765 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000766 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000767 AssertOK();
768}
769
770StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000771 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772 Ops[0].init(val, this);
773 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000774 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 AssertOK();
776}
777
Misha Brukmanb1c93172005-04-21 23:48:37 +0000778StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000780 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781 Ops[0].init(val, this);
782 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000783 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784 AssertOK();
785}
786
Misha Brukmanb1c93172005-04-21 23:48:37 +0000787StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000789 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790 Ops[0].init(val, this);
791 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000792 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794}
795
796//===----------------------------------------------------------------------===//
797// GetElementPtrInst Implementation
798//===----------------------------------------------------------------------===//
799
800// checkType - Simple wrapper function to give a better assertion failure
801// message on bad indexes for a gep instruction.
802//
803static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000804 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805 return Ty;
806}
807
Chris Lattner79807c3d2007-01-31 19:47:18 +0000808void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
809 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 Use *OL = OperandList = new Use[NumOperands];
811 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812
Chris Lattner79807c3d2007-01-31 19:47:18 +0000813 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000814 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000815}
816
817void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 NumOperands = 3;
819 Use *OL = OperandList = new Use[3];
820 OL[0].init(Ptr, this);
821 OL[1].init(Idx0, this);
822 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000823}
824
Chris Lattner82981202005-05-03 05:43:30 +0000825void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
826 NumOperands = 2;
827 Use *OL = OperandList = new Use[2];
828 OL[0].init(Ptr, this);
829 OL[1].init(Idx, this);
830}
831
Chris Lattner79807c3d2007-01-31 19:47:18 +0000832
833GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
834 unsigned NumIdx,
835 const std::string &Name, Instruction *InBe)
836: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000837 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000838 GetElementPtr, 0, 0, InBe) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000839 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000840 setName(Name);
Chris Lattner79807c3d2007-01-31 19:47:18 +0000841}
842
843GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
844 unsigned NumIdx,
845 const std::string &Name, BasicBlock *IAE)
846: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000847 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000848 GetElementPtr, 0, 0, IAE) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000849 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000850 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
Chris Lattner82981202005-05-03 05:43:30 +0000853GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
854 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000855 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
856 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000857 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000858 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000859}
860
861GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
862 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000863 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
864 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000865 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000867}
868
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000869GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
870 const std::string &Name, Instruction *InBe)
871 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
872 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000873 GetElementPtr, 0, 0, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000876}
877
878GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000879 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
881 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 GetElementPtr, 0, 0, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000884 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885}
886
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887GetElementPtrInst::~GetElementPtrInst() {
888 delete[] OperandList;
889}
890
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000891// getIndexedType - Returns the type of the element that would be loaded with
892// a load instruction with the specified parameters.
893//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000894// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000895// pointer type.
896//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000897const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000898 Value* const *Idxs,
899 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900 bool AllowCompositeLeaf) {
901 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
902
903 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000904 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905 if (AllowCompositeLeaf ||
906 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
907 return cast<PointerType>(Ptr)->getElementType();
908 else
909 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000910
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911 unsigned CurIdx = 0;
912 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000913 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
915 return 0; // Can't load a whole structure or array!?!?
916 }
917
Chris Lattner302116a2007-01-31 04:40:28 +0000918 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000919 if (isa<PointerType>(CT) && CurIdx != 1)
920 return 0; // Can only index into pointer types at the first index!
921 if (!CT->indexValid(Index)) return 0;
922 Ptr = CT->getTypeAtIndex(Index);
923
924 // If the new type forwards to another type, then it is in the middle
925 // of being refined to another type (and hence, may have dropped all
926 // references to what it was using before). So, use the new forwarded
927 // type.
928 if (const Type * Ty = Ptr->getForwardedType()) {
929 Ptr = Ty;
930 }
931 }
Chris Lattner302116a2007-01-31 04:40:28 +0000932 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000933}
934
Misha Brukmanb1c93172005-04-21 23:48:37 +0000935const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000936 Value *Idx0, Value *Idx1,
937 bool AllowCompositeLeaf) {
938 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
939 if (!PTy) return 0; // Type isn't a pointer type!
940
941 // Check the pointer index.
942 if (!PTy->indexValid(Idx0)) return 0;
943
944 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
945 if (!CT || !CT->indexValid(Idx1)) return 0;
946
947 const Type *ElTy = CT->getTypeAtIndex(Idx1);
948 if (AllowCompositeLeaf || ElTy->isFirstClassType())
949 return ElTy;
950 return 0;
951}
952
Chris Lattner82981202005-05-03 05:43:30 +0000953const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
954 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
955 if (!PTy) return 0; // Type isn't a pointer type!
956
957 // Check the pointer index.
958 if (!PTy->indexValid(Idx)) return 0;
959
Chris Lattnerc2233332005-05-03 16:44:45 +0000960 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000961}
962
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000964// ExtractElementInst Implementation
965//===----------------------------------------------------------------------===//
966
967ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000968 const std::string &Name,
969 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000970 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000971 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000972 assert(isValidOperands(Val, Index) &&
973 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000974 Ops[0].init(Val, this);
975 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000976 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +0000977}
978
Chris Lattner65511ff2006-10-05 06:24:58 +0000979ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
980 const std::string &Name,
981 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000982 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000983 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000984 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000985 assert(isValidOperands(Val, Index) &&
986 "Invalid extractelement instruction operands!");
987 Ops[0].init(Val, this);
988 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000989 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +0000990}
991
992
Robert Bocchino23004482006-01-10 19:05:34 +0000993ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000994 const std::string &Name,
995 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000996 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000997 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000998 assert(isValidOperands(Val, Index) &&
999 "Invalid extractelement instruction operands!");
1000
Robert Bocchino23004482006-01-10 19:05:34 +00001001 Ops[0].init(Val, this);
1002 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001003 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001004}
1005
Chris Lattner65511ff2006-10-05 06:24:58 +00001006ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1007 const std::string &Name,
1008 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001009 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001010 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001011 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001012 assert(isValidOperands(Val, Index) &&
1013 "Invalid extractelement instruction operands!");
1014
1015 Ops[0].init(Val, this);
1016 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001017 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001018}
1019
1020
Chris Lattner54865b32006-04-08 04:05:48 +00001021bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001022 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001023 return false;
1024 return true;
1025}
1026
1027
Robert Bocchino23004482006-01-10 19:05:34 +00001028//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001029// InsertElementInst Implementation
1030//===----------------------------------------------------------------------===//
1031
Chris Lattner0875d942006-04-14 22:20:32 +00001032InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1033 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1034 Ops[0].init(IE.Ops[0], this);
1035 Ops[1].init(IE.Ops[1], this);
1036 Ops[2].init(IE.Ops[2], this);
1037}
Chris Lattner54865b32006-04-08 04:05:48 +00001038InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001039 const std::string &Name,
1040 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001041 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001042 assert(isValidOperands(Vec, Elt, Index) &&
1043 "Invalid insertelement instruction operands!");
1044 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001045 Ops[1].init(Elt, this);
1046 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001047 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001048}
1049
Chris Lattner65511ff2006-10-05 06:24:58 +00001050InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1051 const std::string &Name,
1052 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001053 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001054 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001055 assert(isValidOperands(Vec, Elt, Index) &&
1056 "Invalid insertelement instruction operands!");
1057 Ops[0].init(Vec, this);
1058 Ops[1].init(Elt, this);
1059 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001060 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001061}
1062
1063
Chris Lattner54865b32006-04-08 04:05:48 +00001064InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001065 const std::string &Name,
1066 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001067 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001068 assert(isValidOperands(Vec, Elt, Index) &&
1069 "Invalid insertelement instruction operands!");
1070
1071 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001072 Ops[1].init(Elt, this);
1073 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001074 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001075}
1076
Chris Lattner65511ff2006-10-05 06:24:58 +00001077InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1078 const std::string &Name,
1079 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001080: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001081 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001082 assert(isValidOperands(Vec, Elt, Index) &&
1083 "Invalid insertelement instruction operands!");
1084
1085 Ops[0].init(Vec, this);
1086 Ops[1].init(Elt, this);
1087 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001088 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001089}
1090
Chris Lattner54865b32006-04-08 04:05:48 +00001091bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1092 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001093 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001094 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001095
Reid Spencerd84d35b2007-02-15 02:26:10 +00001096 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Chris Lattner54865b32006-04-08 04:05:48 +00001097 return false;// Second operand of insertelement must be packed element type.
1098
Reid Spencer8d9336d2006-12-31 05:26:44 +00001099 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001100 return false; // Third operand of insertelement must be uint.
1101 return true;
1102}
1103
1104
Robert Bocchinoca27f032006-01-17 20:07:22 +00001105//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001106// ShuffleVectorInst Implementation
1107//===----------------------------------------------------------------------===//
1108
Chris Lattner0875d942006-04-14 22:20:32 +00001109ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1110 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1111 Ops[0].init(SV.Ops[0], this);
1112 Ops[1].init(SV.Ops[1], this);
1113 Ops[2].init(SV.Ops[2], this);
1114}
1115
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001116ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1117 const std::string &Name,
1118 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001119 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001120 assert(isValidOperands(V1, V2, Mask) &&
1121 "Invalid shuffle vector instruction operands!");
1122 Ops[0].init(V1, this);
1123 Ops[1].init(V2, this);
1124 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001125 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001126}
1127
1128ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1129 const std::string &Name,
1130 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001131 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001132 assert(isValidOperands(V1, V2, Mask) &&
1133 "Invalid shuffle vector instruction operands!");
1134
1135 Ops[0].init(V1, this);
1136 Ops[1].init(V2, this);
1137 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001138 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001139}
1140
1141bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1142 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001143 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001144 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001145 if (!isa<VectorType>(Mask->getType()) ||
1146 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1147 cast<VectorType>(Mask->getType())->getNumElements() !=
1148 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001149 return false;
1150 return true;
1151}
1152
1153
1154//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155// BinaryOperator Class
1156//===----------------------------------------------------------------------===//
1157
Chris Lattner2195fc42007-02-24 00:55:48 +00001158BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1159 const Type *Ty, const std::string &Name,
1160 Instruction *InsertBefore)
1161 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1162 Ops[0].init(S1, this);
1163 Ops[1].init(S2, this);
1164 init(iType);
1165 setName(Name);
1166}
1167
1168BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1169 const Type *Ty, const std::string &Name,
1170 BasicBlock *InsertAtEnd)
1171 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1172 Ops[0].init(S1, this);
1173 Ops[1].init(S2, this);
1174 init(iType);
1175 setName(Name);
1176}
1177
1178
1179void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001180 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001181 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001182 assert(LHS->getType() == RHS->getType() &&
1183 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184#ifndef NDEBUG
1185 switch (iType) {
1186 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001187 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001188 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001189 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001190 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001191 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001192 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001194 case UDiv:
1195 case SDiv:
1196 assert(getType() == LHS->getType() &&
1197 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001198 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1199 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001200 "Incorrect operand type (not integer) for S/UDIV");
1201 break;
1202 case FDiv:
1203 assert(getType() == LHS->getType() &&
1204 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001205 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1206 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001207 && "Incorrect operand type (not floating point) for FDIV");
1208 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001209 case URem:
1210 case SRem:
1211 assert(getType() == LHS->getType() &&
1212 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001213 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1214 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001215 "Incorrect operand type (not integer) for S/UREM");
1216 break;
1217 case FRem:
1218 assert(getType() == LHS->getType() &&
1219 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001220 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1221 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001222 && "Incorrect operand type (not floating point) for FREM");
1223 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001224 case Shl:
1225 case LShr:
1226 case AShr:
1227 assert(getType() == LHS->getType() &&
1228 "Shift operation should return same type as operands!");
1229 assert(getType()->isInteger() &&
1230 "Shift operation requires integer operands");
1231 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001232 case And: case Or:
1233 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001234 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001235 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001236 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001237 (isa<VectorType>(getType()) &&
1238 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001239 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001240 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001241 default:
1242 break;
1243 }
1244#endif
1245}
1246
1247BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001248 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001249 Instruction *InsertBefore) {
1250 assert(S1->getType() == S2->getType() &&
1251 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001252 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001253}
1254
1255BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001256 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001257 BasicBlock *InsertAtEnd) {
1258 BinaryOperator *Res = create(Op, S1, S2, Name);
1259 InsertAtEnd->getInstList().push_back(Res);
1260 return Res;
1261}
1262
1263BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1264 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001265 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1266 return new BinaryOperator(Instruction::Sub,
1267 zero, Op,
1268 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001269}
1270
1271BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1272 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001273 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1274 return new BinaryOperator(Instruction::Sub,
1275 zero, Op,
1276 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001277}
1278
1279BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1280 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001281 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001282 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001283 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001284 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001285 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001286 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001287 }
1288
1289 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001290 Op->getType(), Name, InsertBefore);
1291}
1292
1293BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1294 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001295 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001296 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001297 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001298 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001299 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001300 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001301 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001302 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001303 }
1304
1305 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001306 Op->getType(), Name, InsertAtEnd);
1307}
1308
1309
1310// isConstantAllOnes - Helper function for several functions below
1311static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001312 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001313}
1314
1315bool BinaryOperator::isNeg(const Value *V) {
1316 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1317 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001318 return Bop->getOperand(0) ==
1319 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001320 return false;
1321}
1322
1323bool BinaryOperator::isNot(const Value *V) {
1324 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1325 return (Bop->getOpcode() == Instruction::Xor &&
1326 (isConstantAllOnes(Bop->getOperand(1)) ||
1327 isConstantAllOnes(Bop->getOperand(0))));
1328 return false;
1329}
1330
Chris Lattner2c7d1772005-04-24 07:28:37 +00001331Value *BinaryOperator::getNegArgument(Value *BinOp) {
1332 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1333 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334}
1335
Chris Lattner2c7d1772005-04-24 07:28:37 +00001336const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1337 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338}
1339
Chris Lattner2c7d1772005-04-24 07:28:37 +00001340Value *BinaryOperator::getNotArgument(Value *BinOp) {
1341 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1342 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1343 Value *Op0 = BO->getOperand(0);
1344 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001345 if (isConstantAllOnes(Op0)) return Op1;
1346
1347 assert(isConstantAllOnes(Op1));
1348 return Op0;
1349}
1350
Chris Lattner2c7d1772005-04-24 07:28:37 +00001351const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1352 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001353}
1354
1355
1356// swapOperands - Exchange the two operands to this instruction. This
1357// instruction is safe to use on any binary instruction and does not
1358// modify the semantics of the instruction. If the instruction is
1359// order dependent (SetLT f.e.) the opcode is changed.
1360//
1361bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001362 if (!isCommutative())
1363 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001364 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001365 return false;
1366}
1367
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001368//===----------------------------------------------------------------------===//
1369// CastInst Class
1370//===----------------------------------------------------------------------===//
1371
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001372// Just determine if this cast only deals with integral->integral conversion.
1373bool CastInst::isIntegerCast() const {
1374 switch (getOpcode()) {
1375 default: return false;
1376 case Instruction::ZExt:
1377 case Instruction::SExt:
1378 case Instruction::Trunc:
1379 return true;
1380 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001381 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001382 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001383}
1384
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001385bool CastInst::isLosslessCast() const {
1386 // Only BitCast can be lossless, exit fast if we're not BitCast
1387 if (getOpcode() != Instruction::BitCast)
1388 return false;
1389
1390 // Identity cast is always lossless
1391 const Type* SrcTy = getOperand(0)->getType();
1392 const Type* DstTy = getType();
1393 if (SrcTy == DstTy)
1394 return true;
1395
Reid Spencer8d9336d2006-12-31 05:26:44 +00001396 // Pointer to pointer is always lossless.
1397 if (isa<PointerType>(SrcTy))
1398 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001399 return false; // Other types have no identity values
1400}
1401
1402/// This function determines if the CastInst does not require any bits to be
1403/// changed in order to effect the cast. Essentially, it identifies cases where
1404/// no code gen is necessary for the cast, hence the name no-op cast. For
1405/// example, the following are all no-op casts:
1406/// # bitcast uint %X, int
1407/// # bitcast uint* %x, sbyte*
1408/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1409/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1410/// @brief Determine if a cast is a no-op.
1411bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1412 switch (getOpcode()) {
1413 default:
1414 assert(!"Invalid CastOp");
1415 case Instruction::Trunc:
1416 case Instruction::ZExt:
1417 case Instruction::SExt:
1418 case Instruction::FPTrunc:
1419 case Instruction::FPExt:
1420 case Instruction::UIToFP:
1421 case Instruction::SIToFP:
1422 case Instruction::FPToUI:
1423 case Instruction::FPToSI:
1424 return false; // These always modify bits
1425 case Instruction::BitCast:
1426 return true; // BitCast never modifies bits.
1427 case Instruction::PtrToInt:
1428 return IntPtrTy->getPrimitiveSizeInBits() ==
1429 getType()->getPrimitiveSizeInBits();
1430 case Instruction::IntToPtr:
1431 return IntPtrTy->getPrimitiveSizeInBits() ==
1432 getOperand(0)->getType()->getPrimitiveSizeInBits();
1433 }
1434}
1435
1436/// This function determines if a pair of casts can be eliminated and what
1437/// opcode should be used in the elimination. This assumes that there are two
1438/// instructions like this:
1439/// * %F = firstOpcode SrcTy %x to MidTy
1440/// * %S = secondOpcode MidTy %F to DstTy
1441/// The function returns a resultOpcode so these two casts can be replaced with:
1442/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1443/// If no such cast is permited, the function returns 0.
1444unsigned CastInst::isEliminableCastPair(
1445 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1446 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1447{
1448 // Define the 144 possibilities for these two cast instructions. The values
1449 // in this matrix determine what to do in a given situation and select the
1450 // case in the switch below. The rows correspond to firstOp, the columns
1451 // correspond to secondOp. In looking at the table below, keep in mind
1452 // the following cast properties:
1453 //
1454 // Size Compare Source Destination
1455 // Operator Src ? Size Type Sign Type Sign
1456 // -------- ------------ ------------------- ---------------------
1457 // TRUNC > Integer Any Integral Any
1458 // ZEXT < Integral Unsigned Integer Any
1459 // SEXT < Integral Signed Integer Any
1460 // FPTOUI n/a FloatPt n/a Integral Unsigned
1461 // FPTOSI n/a FloatPt n/a Integral Signed
1462 // UITOFP n/a Integral Unsigned FloatPt n/a
1463 // SITOFP n/a Integral Signed FloatPt n/a
1464 // FPTRUNC > FloatPt n/a FloatPt n/a
1465 // FPEXT < FloatPt n/a FloatPt n/a
1466 // PTRTOINT n/a Pointer n/a Integral Unsigned
1467 // INTTOPTR n/a Integral Unsigned Pointer n/a
1468 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001469 //
1470 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1471 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1472 // into "fptoui double to ulong", but this loses information about the range
1473 // of the produced value (we no longer know the top-part is all zeros).
1474 // Further this conversion is often much more expensive for typical hardware,
1475 // and causes issues when building libgcc. We disallow fptosi+sext for the
1476 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001477 const unsigned numCastOps =
1478 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1479 static const uint8_t CastResults[numCastOps][numCastOps] = {
1480 // T F F U S F F P I B -+
1481 // R Z S P P I I T P 2 N T |
1482 // U E E 2 2 2 2 R E I T C +- secondOp
1483 // N X X U S F F N X N 2 V |
1484 // C T T I I P P C T T P T -+
1485 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1486 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1487 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001488 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1489 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001490 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1491 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1492 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1493 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1494 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1495 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1496 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1497 };
1498
1499 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1500 [secondOp-Instruction::CastOpsBegin];
1501 switch (ElimCase) {
1502 case 0:
1503 // categorically disallowed
1504 return 0;
1505 case 1:
1506 // allowed, use first cast's opcode
1507 return firstOp;
1508 case 2:
1509 // allowed, use second cast's opcode
1510 return secondOp;
1511 case 3:
1512 // no-op cast in second op implies firstOp as long as the DestTy
1513 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001514 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001515 return firstOp;
1516 return 0;
1517 case 4:
1518 // no-op cast in second op implies firstOp as long as the DestTy
1519 // is floating point
1520 if (DstTy->isFloatingPoint())
1521 return firstOp;
1522 return 0;
1523 case 5:
1524 // no-op cast in first op implies secondOp as long as the SrcTy
1525 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001526 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001527 return secondOp;
1528 return 0;
1529 case 6:
1530 // no-op cast in first op implies secondOp as long as the SrcTy
1531 // is a floating point
1532 if (SrcTy->isFloatingPoint())
1533 return secondOp;
1534 return 0;
1535 case 7: {
1536 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1537 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1538 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1539 if (MidSize >= PtrSize)
1540 return Instruction::BitCast;
1541 return 0;
1542 }
1543 case 8: {
1544 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1545 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1546 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1547 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1548 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1549 if (SrcSize == DstSize)
1550 return Instruction::BitCast;
1551 else if (SrcSize < DstSize)
1552 return firstOp;
1553 return secondOp;
1554 }
1555 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1556 return Instruction::ZExt;
1557 case 10:
1558 // fpext followed by ftrunc is allowed if the bit size returned to is
1559 // the same as the original, in which case its just a bitcast
1560 if (SrcTy == DstTy)
1561 return Instruction::BitCast;
1562 return 0; // If the types are not the same we can't eliminate it.
1563 case 11:
1564 // bitcast followed by ptrtoint is allowed as long as the bitcast
1565 // is a pointer to pointer cast.
1566 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1567 return secondOp;
1568 return 0;
1569 case 12:
1570 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1571 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1572 return firstOp;
1573 return 0;
1574 case 13: {
1575 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1576 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1577 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1578 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1579 if (SrcSize <= PtrSize && SrcSize == DstSize)
1580 return Instruction::BitCast;
1581 return 0;
1582 }
1583 case 99:
1584 // cast combination can't happen (error in input). This is for all cases
1585 // where the MidTy is not the same for the two cast instructions.
1586 assert(!"Invalid Cast Combination");
1587 return 0;
1588 default:
1589 assert(!"Error in CastResults table!!!");
1590 return 0;
1591 }
1592 return 0;
1593}
1594
1595CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1596 const std::string &Name, Instruction *InsertBefore) {
1597 // Construct and return the appropriate CastInst subclass
1598 switch (op) {
1599 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1600 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1601 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1602 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1603 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1604 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1605 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1606 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1607 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1608 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1609 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1610 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1611 default:
1612 assert(!"Invalid opcode provided");
1613 }
1614 return 0;
1615}
1616
1617CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1618 const std::string &Name, BasicBlock *InsertAtEnd) {
1619 // Construct and return the appropriate CastInst subclass
1620 switch (op) {
1621 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1622 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1623 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1624 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1625 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1626 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1627 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1628 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1629 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1630 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1631 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1632 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1633 default:
1634 assert(!"Invalid opcode provided");
1635 }
1636 return 0;
1637}
1638
Reid Spencer5c140882006-12-04 20:17:56 +00001639CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1640 const std::string &Name,
1641 Instruction *InsertBefore) {
1642 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1643 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1644 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1645}
1646
1647CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1648 const std::string &Name,
1649 BasicBlock *InsertAtEnd) {
1650 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1651 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1652 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1653}
1654
1655CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1656 const std::string &Name,
1657 Instruction *InsertBefore) {
1658 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1659 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1660 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1661}
1662
1663CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1664 const std::string &Name,
1665 BasicBlock *InsertAtEnd) {
1666 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1667 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1668 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1669}
1670
1671CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1672 const std::string &Name,
1673 Instruction *InsertBefore) {
1674 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1675 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1676 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1677}
1678
1679CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1680 const std::string &Name,
1681 BasicBlock *InsertAtEnd) {
1682 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1683 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1684 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1685}
1686
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001687CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1688 const std::string &Name,
1689 BasicBlock *InsertAtEnd) {
1690 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001691 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001692 "Invalid cast");
1693
Chris Lattner03c49532007-01-15 02:27:26 +00001694 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001695 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1696 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1697}
1698
1699/// @brief Create a BitCast or a PtrToInt cast instruction
1700CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1701 const std::string &Name,
1702 Instruction *InsertBefore) {
1703 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001704 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001705 "Invalid cast");
1706
Chris Lattner03c49532007-01-15 02:27:26 +00001707 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001708 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1709 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1710}
1711
Reid Spencer7e933472006-12-12 00:49:44 +00001712CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1713 bool isSigned, const std::string &Name,
1714 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001715 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001716 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1717 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1718 Instruction::CastOps opcode =
1719 (SrcBits == DstBits ? Instruction::BitCast :
1720 (SrcBits > DstBits ? Instruction::Trunc :
1721 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1722 return create(opcode, C, Ty, Name, InsertBefore);
1723}
1724
1725CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1726 bool isSigned, const std::string &Name,
1727 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001728 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001729 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1730 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1731 Instruction::CastOps opcode =
1732 (SrcBits == DstBits ? Instruction::BitCast :
1733 (SrcBits > DstBits ? Instruction::Trunc :
1734 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1735 return create(opcode, C, Ty, Name, InsertAtEnd);
1736}
1737
1738CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1739 const std::string &Name,
1740 Instruction *InsertBefore) {
1741 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1742 "Invalid cast");
1743 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1744 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1745 Instruction::CastOps opcode =
1746 (SrcBits == DstBits ? Instruction::BitCast :
1747 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1748 return create(opcode, C, Ty, Name, InsertBefore);
1749}
1750
1751CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1752 const std::string &Name,
1753 BasicBlock *InsertAtEnd) {
1754 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1755 "Invalid cast");
1756 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1757 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1758 Instruction::CastOps opcode =
1759 (SrcBits == DstBits ? Instruction::BitCast :
1760 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1761 return create(opcode, C, Ty, Name, InsertAtEnd);
1762}
1763
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001764// Provide a way to get a "cast" where the cast opcode is inferred from the
1765// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001766// logic in the castIsValid function below. This axiom should hold:
1767// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1768// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001769// casting opcode for the arguments passed to it.
1770Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001771CastInst::getCastOpcode(
1772 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001773 // Get the bit sizes, we'll need these
1774 const Type *SrcTy = Src->getType();
1775 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1776 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1777
1778 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001779 if (DestTy->isInteger()) { // Casting to integral
1780 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001781 if (DestBits < SrcBits)
1782 return Trunc; // int -> smaller int
1783 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001784 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001785 return SExt; // signed -> SEXT
1786 else
1787 return ZExt; // unsigned -> ZEXT
1788 } else {
1789 return BitCast; // Same size, No-op cast
1790 }
1791 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001792 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 return FPToSI; // FP -> sint
1794 else
1795 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001796 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001797 assert(DestBits == PTy->getBitWidth() &&
1798 "Casting packed to integer of different width");
1799 return BitCast; // Same size, no-op cast
1800 } else {
1801 assert(isa<PointerType>(SrcTy) &&
1802 "Casting from a value that is not first-class type");
1803 return PtrToInt; // ptr -> int
1804 }
1805 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001806 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001807 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001808 return SIToFP; // sint -> FP
1809 else
1810 return UIToFP; // uint -> FP
1811 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1812 if (DestBits < SrcBits) {
1813 return FPTrunc; // FP -> smaller FP
1814 } else if (DestBits > SrcBits) {
1815 return FPExt; // FP -> larger FP
1816 } else {
1817 return BitCast; // same size, no-op cast
1818 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001819 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001820 assert(DestBits == PTy->getBitWidth() &&
1821 "Casting packed to floating point of different width");
1822 return BitCast; // same size, no-op cast
1823 } else {
1824 assert(0 && "Casting pointer or non-first class to float");
1825 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001826 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1827 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001828 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1829 "Casting packed to packed of different widths");
1830 return BitCast; // packed -> packed
1831 } else if (DestPTy->getBitWidth() == SrcBits) {
1832 return BitCast; // float/int -> packed
1833 } else {
1834 assert(!"Illegal cast to packed (wrong type or size)");
1835 }
1836 } else if (isa<PointerType>(DestTy)) {
1837 if (isa<PointerType>(SrcTy)) {
1838 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001839 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840 return IntToPtr; // int -> ptr
1841 } else {
1842 assert(!"Casting pointer to other than pointer or int");
1843 }
1844 } else {
1845 assert(!"Casting to type that is not first-class");
1846 }
1847
1848 // If we fall through to here we probably hit an assertion cast above
1849 // and assertions are not turned on. Anything we return is an error, so
1850 // BitCast is as good a choice as any.
1851 return BitCast;
1852}
1853
1854//===----------------------------------------------------------------------===//
1855// CastInst SubClass Constructors
1856//===----------------------------------------------------------------------===//
1857
1858/// Check that the construction parameters for a CastInst are correct. This
1859/// could be broken out into the separate constructors but it is useful to have
1860/// it in one place and to eliminate the redundant code for getting the sizes
1861/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001862bool
1863CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001864
1865 // Check for type sanity on the arguments
1866 const Type *SrcTy = S->getType();
1867 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1868 return false;
1869
1870 // Get the size of the types in bits, we'll need this later
1871 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1872 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1873
1874 // Switch on the opcode provided
1875 switch (op) {
1876 default: return false; // This is an input error
1877 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001878 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001880 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001882 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001883 case Instruction::FPTrunc:
1884 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1885 SrcBitSize > DstBitSize;
1886 case Instruction::FPExt:
1887 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1888 SrcBitSize < DstBitSize;
1889 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001890 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001891 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001892 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001893 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001894 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001895 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001896 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001897 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001898 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001900 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 case Instruction::BitCast:
1902 // BitCast implies a no-op cast of type only. No bits change.
1903 // However, you can't cast pointers to anything but pointers.
1904 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1905 return false;
1906
1907 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1908 // these cases, the cast is okay if the source and destination bit widths
1909 // are identical.
1910 return SrcBitSize == DstBitSize;
1911 }
1912}
1913
1914TruncInst::TruncInst(
1915 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1916) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001917 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001918}
1919
1920TruncInst::TruncInst(
1921 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1922) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001923 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924}
1925
1926ZExtInst::ZExtInst(
1927 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1928) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001929 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001930}
1931
1932ZExtInst::ZExtInst(
1933 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1934) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001935 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936}
1937SExtInst::SExtInst(
1938 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1939) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001940 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941}
1942
Jeff Cohencc08c832006-12-02 02:22:01 +00001943SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001944 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1945) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001946 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947}
1948
1949FPTruncInst::FPTruncInst(
1950 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1951) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001952 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953}
1954
1955FPTruncInst::FPTruncInst(
1956 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1957) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001958 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959}
1960
1961FPExtInst::FPExtInst(
1962 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1963) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001964 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965}
1966
1967FPExtInst::FPExtInst(
1968 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1969) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001970 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971}
1972
1973UIToFPInst::UIToFPInst(
1974 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1975) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001976 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977}
1978
1979UIToFPInst::UIToFPInst(
1980 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1981) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001982 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001983}
1984
1985SIToFPInst::SIToFPInst(
1986 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1987) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001988 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001989}
1990
1991SIToFPInst::SIToFPInst(
1992 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1993) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001994 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995}
1996
1997FPToUIInst::FPToUIInst(
1998 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1999) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002000 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001}
2002
2003FPToUIInst::FPToUIInst(
2004 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2005) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002006 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007}
2008
2009FPToSIInst::FPToSIInst(
2010 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2011) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002012 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013}
2014
2015FPToSIInst::FPToSIInst(
2016 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2017) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002018 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002019}
2020
2021PtrToIntInst::PtrToIntInst(
2022 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2023) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002024 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002025}
2026
2027PtrToIntInst::PtrToIntInst(
2028 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2029) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002030 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002031}
2032
2033IntToPtrInst::IntToPtrInst(
2034 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2035) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002036 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037}
2038
2039IntToPtrInst::IntToPtrInst(
2040 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2041) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002042 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043}
2044
2045BitCastInst::BitCastInst(
2046 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2047) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002048 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049}
2050
2051BitCastInst::BitCastInst(
2052 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2053) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002054 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002056
2057//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002058// CmpInst Classes
2059//===----------------------------------------------------------------------===//
2060
2061CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2062 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002063 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002064 Ops[0].init(LHS, this);
2065 Ops[1].init(RHS, this);
2066 SubclassData = predicate;
2067 if (op == Instruction::ICmp) {
2068 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2069 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2070 "Invalid ICmp predicate value");
2071 const Type* Op0Ty = getOperand(0)->getType();
2072 const Type* Op1Ty = getOperand(1)->getType();
2073 assert(Op0Ty == Op1Ty &&
2074 "Both operands to ICmp instruction are not of the same type!");
2075 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002076 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002077 "Invalid operand types for ICmp instruction");
2078 return;
2079 }
2080 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2081 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2082 "Invalid FCmp predicate value");
2083 const Type* Op0Ty = getOperand(0)->getType();
2084 const Type* Op1Ty = getOperand(1)->getType();
2085 assert(Op0Ty == Op1Ty &&
2086 "Both operands to FCmp instruction are not of the same type!");
2087 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002088 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002089 "Invalid operand types for FCmp instruction");
Chris Lattner2195fc42007-02-24 00:55:48 +00002090 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002091}
2092
2093CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2094 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002095 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002096 Ops[0].init(LHS, this);
2097 Ops[1].init(RHS, this);
2098 SubclassData = predicate;
2099 if (op == Instruction::ICmp) {
2100 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2101 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2102 "Invalid ICmp predicate value");
2103
2104 const Type* Op0Ty = getOperand(0)->getType();
2105 const Type* Op1Ty = getOperand(1)->getType();
2106 assert(Op0Ty == Op1Ty &&
2107 "Both operands to ICmp instruction are not of the same type!");
2108 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002109 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002110 "Invalid operand types for ICmp instruction");
2111 return;
2112 }
2113 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2114 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2115 "Invalid FCmp predicate value");
2116 const Type* Op0Ty = getOperand(0)->getType();
2117 const Type* Op1Ty = getOperand(1)->getType();
2118 assert(Op0Ty == Op1Ty &&
2119 "Both operands to FCmp instruction are not of the same type!");
2120 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002121 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002122 "Invalid operand types for FCmp instruction");
Chris Lattner2195fc42007-02-24 00:55:48 +00002123 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002124}
2125
2126CmpInst *
2127CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2128 const std::string &Name, Instruction *InsertBefore) {
2129 if (Op == Instruction::ICmp) {
2130 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2131 InsertBefore);
2132 }
2133 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2134 InsertBefore);
2135}
2136
2137CmpInst *
2138CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2139 const std::string &Name, BasicBlock *InsertAtEnd) {
2140 if (Op == Instruction::ICmp) {
2141 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2142 InsertAtEnd);
2143 }
2144 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2145 InsertAtEnd);
2146}
2147
2148void CmpInst::swapOperands() {
2149 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2150 IC->swapOperands();
2151 else
2152 cast<FCmpInst>(this)->swapOperands();
2153}
2154
2155bool CmpInst::isCommutative() {
2156 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2157 return IC->isCommutative();
2158 return cast<FCmpInst>(this)->isCommutative();
2159}
2160
2161bool CmpInst::isEquality() {
2162 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2163 return IC->isEquality();
2164 return cast<FCmpInst>(this)->isEquality();
2165}
2166
2167
2168ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2169 switch (pred) {
2170 default:
2171 assert(!"Unknown icmp predicate!");
2172 case ICMP_EQ: return ICMP_NE;
2173 case ICMP_NE: return ICMP_EQ;
2174 case ICMP_UGT: return ICMP_ULE;
2175 case ICMP_ULT: return ICMP_UGE;
2176 case ICMP_UGE: return ICMP_ULT;
2177 case ICMP_ULE: return ICMP_UGT;
2178 case ICMP_SGT: return ICMP_SLE;
2179 case ICMP_SLT: return ICMP_SGE;
2180 case ICMP_SGE: return ICMP_SLT;
2181 case ICMP_SLE: return ICMP_SGT;
2182 }
2183}
2184
2185ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2186 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002187 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002188 case ICMP_EQ: case ICMP_NE:
2189 return pred;
2190 case ICMP_SGT: return ICMP_SLT;
2191 case ICMP_SLT: return ICMP_SGT;
2192 case ICMP_SGE: return ICMP_SLE;
2193 case ICMP_SLE: return ICMP_SGE;
2194 case ICMP_UGT: return ICMP_ULT;
2195 case ICMP_ULT: return ICMP_UGT;
2196 case ICMP_UGE: return ICMP_ULE;
2197 case ICMP_ULE: return ICMP_UGE;
2198 }
2199}
2200
Reid Spencer266e42b2006-12-23 06:05:41 +00002201ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2202 switch (pred) {
2203 default: assert(! "Unknown icmp predicate!");
2204 case ICMP_EQ: case ICMP_NE:
2205 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2206 return pred;
2207 case ICMP_UGT: return ICMP_SGT;
2208 case ICMP_ULT: return ICMP_SLT;
2209 case ICMP_UGE: return ICMP_SGE;
2210 case ICMP_ULE: return ICMP_SLE;
2211 }
2212}
2213
2214bool ICmpInst::isSignedPredicate(Predicate pred) {
2215 switch (pred) {
2216 default: assert(! "Unknown icmp predicate!");
2217 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2218 return true;
2219 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2220 case ICMP_UGE: case ICMP_ULE:
2221 return false;
2222 }
2223}
2224
Reid Spencer0286bc12007-02-28 22:00:54 +00002225/// Initialize a set of values that all satisfy the condition with C.
2226///
2227ConstantRange
2228ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2229 APInt Lower(C);
2230 APInt Upper(C);
2231 uint32_t BitWidth = C.getBitWidth();
2232 switch (pred) {
2233 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2234 case ICmpInst::ICMP_EQ: Upper++; break;
2235 case ICmpInst::ICMP_NE: Lower++; break;
2236 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2237 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2238 case ICmpInst::ICMP_UGT:
2239 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2240 break;
2241 case ICmpInst::ICMP_SGT:
2242 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2243 break;
2244 case ICmpInst::ICMP_ULE:
2245 Lower = APInt::getMinValue(BitWidth); Upper++;
2246 break;
2247 case ICmpInst::ICMP_SLE:
2248 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2249 break;
2250 case ICmpInst::ICMP_UGE:
2251 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2252 break;
2253 case ICmpInst::ICMP_SGE:
2254 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2255 break;
2256 }
2257 return ConstantRange(Lower, Upper);
2258}
2259
Reid Spencerd9436b62006-11-20 01:22:35 +00002260FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2261 switch (pred) {
2262 default:
2263 assert(!"Unknown icmp predicate!");
2264 case FCMP_OEQ: return FCMP_UNE;
2265 case FCMP_ONE: return FCMP_UEQ;
2266 case FCMP_OGT: return FCMP_ULE;
2267 case FCMP_OLT: return FCMP_UGE;
2268 case FCMP_OGE: return FCMP_ULT;
2269 case FCMP_OLE: return FCMP_UGT;
2270 case FCMP_UEQ: return FCMP_ONE;
2271 case FCMP_UNE: return FCMP_OEQ;
2272 case FCMP_UGT: return FCMP_OLE;
2273 case FCMP_ULT: return FCMP_OGE;
2274 case FCMP_UGE: return FCMP_OLT;
2275 case FCMP_ULE: return FCMP_OGT;
2276 case FCMP_ORD: return FCMP_UNO;
2277 case FCMP_UNO: return FCMP_ORD;
2278 case FCMP_TRUE: return FCMP_FALSE;
2279 case FCMP_FALSE: return FCMP_TRUE;
2280 }
2281}
2282
2283FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2284 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002285 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002286 case FCMP_FALSE: case FCMP_TRUE:
2287 case FCMP_OEQ: case FCMP_ONE:
2288 case FCMP_UEQ: case FCMP_UNE:
2289 case FCMP_ORD: case FCMP_UNO:
2290 return pred;
2291 case FCMP_OGT: return FCMP_OLT;
2292 case FCMP_OLT: return FCMP_OGT;
2293 case FCMP_OGE: return FCMP_OLE;
2294 case FCMP_OLE: return FCMP_OGE;
2295 case FCMP_UGT: return FCMP_ULT;
2296 case FCMP_ULT: return FCMP_UGT;
2297 case FCMP_UGE: return FCMP_ULE;
2298 case FCMP_ULE: return FCMP_UGE;
2299 }
2300}
2301
Reid Spencer266e42b2006-12-23 06:05:41 +00002302bool CmpInst::isUnsigned(unsigned short predicate) {
2303 switch (predicate) {
2304 default: return false;
2305 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2306 case ICmpInst::ICMP_UGE: return true;
2307 }
2308}
2309
2310bool CmpInst::isSigned(unsigned short predicate){
2311 switch (predicate) {
2312 default: return false;
2313 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2314 case ICmpInst::ICMP_SGE: return true;
2315 }
2316}
2317
2318bool CmpInst::isOrdered(unsigned short predicate) {
2319 switch (predicate) {
2320 default: return false;
2321 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2322 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2323 case FCmpInst::FCMP_ORD: return true;
2324 }
2325}
2326
2327bool CmpInst::isUnordered(unsigned short predicate) {
2328 switch (predicate) {
2329 default: return false;
2330 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2331 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2332 case FCmpInst::FCMP_UNO: return true;
2333 }
2334}
2335
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002336//===----------------------------------------------------------------------===//
2337// SwitchInst Implementation
2338//===----------------------------------------------------------------------===//
2339
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002340void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002341 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002342 ReservedSpace = 2+NumCases*2;
2343 NumOperands = 2;
2344 OperandList = new Use[ReservedSpace];
2345
2346 OperandList[0].init(Value, this);
2347 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002348}
2349
Chris Lattner2195fc42007-02-24 00:55:48 +00002350/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2351/// switch on and a default destination. The number of additional cases can
2352/// be specified here to make memory allocation more efficient. This
2353/// constructor can also autoinsert before another instruction.
2354SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2355 Instruction *InsertBefore)
2356 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2357 init(Value, Default, NumCases);
2358}
2359
2360/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2361/// switch on and a default destination. The number of additional cases can
2362/// be specified here to make memory allocation more efficient. This
2363/// constructor also autoinserts at the end of the specified BasicBlock.
2364SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2365 BasicBlock *InsertAtEnd)
2366 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2367 init(Value, Default, NumCases);
2368}
2369
Misha Brukmanb1c93172005-04-21 23:48:37 +00002370SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002371 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2372 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002373 Use *OL = OperandList, *InOL = SI.OperandList;
2374 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2375 OL[i].init(InOL[i], this);
2376 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002377 }
2378}
2379
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002380SwitchInst::~SwitchInst() {
2381 delete [] OperandList;
2382}
2383
2384
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002385/// addCase - Add an entry to the switch instruction...
2386///
Chris Lattner47ac1872005-02-24 05:32:09 +00002387void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002388 unsigned OpNo = NumOperands;
2389 if (OpNo+2 > ReservedSpace)
2390 resizeOperands(0); // Get more space!
2391 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002392 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002393 NumOperands = OpNo+2;
2394 OperandList[OpNo].init(OnVal, this);
2395 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002396}
2397
2398/// removeCase - This method removes the specified successor from the switch
2399/// instruction. Note that this cannot be used to remove the default
2400/// destination (successor #0).
2401///
2402void SwitchInst::removeCase(unsigned idx) {
2403 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002404 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2405
2406 unsigned NumOps = getNumOperands();
2407 Use *OL = OperandList;
2408
2409 // Move everything after this operand down.
2410 //
2411 // FIXME: we could just swap with the end of the list, then erase. However,
2412 // client might not expect this to happen. The code as it is thrashes the
2413 // use/def lists, which is kinda lame.
2414 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2415 OL[i-2] = OL[i];
2416 OL[i-2+1] = OL[i+1];
2417 }
2418
2419 // Nuke the last value.
2420 OL[NumOps-2].set(0);
2421 OL[NumOps-2+1].set(0);
2422 NumOperands = NumOps-2;
2423}
2424
2425/// resizeOperands - resize operands - This adjusts the length of the operands
2426/// list according to the following behavior:
2427/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2428/// of operation. This grows the number of ops by 1.5 times.
2429/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2430/// 3. If NumOps == NumOperands, trim the reserved space.
2431///
2432void SwitchInst::resizeOperands(unsigned NumOps) {
2433 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002434 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002435 } else if (NumOps*2 > NumOperands) {
2436 // No resize needed.
2437 if (ReservedSpace >= NumOps) return;
2438 } else if (NumOps == NumOperands) {
2439 if (ReservedSpace == NumOps) return;
2440 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002441 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002442 }
2443
2444 ReservedSpace = NumOps;
2445 Use *NewOps = new Use[NumOps];
2446 Use *OldOps = OperandList;
2447 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2448 NewOps[i].init(OldOps[i], this);
2449 OldOps[i].set(0);
2450 }
2451 delete [] OldOps;
2452 OperandList = NewOps;
2453}
2454
2455
2456BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2457 return getSuccessor(idx);
2458}
2459unsigned SwitchInst::getNumSuccessorsV() const {
2460 return getNumSuccessors();
2461}
2462void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2463 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002464}
Chris Lattnerf22be932004-10-15 23:52:53 +00002465
2466
2467// Define these methods here so vtables don't get emitted into every translation
2468// unit that uses these classes.
2469
2470GetElementPtrInst *GetElementPtrInst::clone() const {
2471 return new GetElementPtrInst(*this);
2472}
2473
2474BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002475 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002476}
2477
Reid Spencerd9436b62006-11-20 01:22:35 +00002478CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002479 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002480}
2481
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2483AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2484FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2485LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2486StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2487CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2488CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2489CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2490CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2491CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2492CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2493CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2494CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2495CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2496CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2497CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2498CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2499CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2501VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2502
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002503ExtractElementInst *ExtractElementInst::clone() const {
2504 return new ExtractElementInst(*this);
2505}
2506InsertElementInst *InsertElementInst::clone() const {
2507 return new InsertElementInst(*this);
2508}
2509ShuffleVectorInst *ShuffleVectorInst::clone() const {
2510 return new ShuffleVectorInst(*this);
2511}
Chris Lattnerf22be932004-10-15 23:52:53 +00002512PHINode *PHINode::clone() const { return new PHINode(*this); }
2513ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2514BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2515SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2516InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2517UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002518UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}