blob: 3bb565d22a7c1abe016fb11a339832f54d96edfe [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"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023using namespace llvm;
24
Chris Lattnerf7b6d312005-05-06 20:26:43 +000025unsigned CallSite::getCallingConv() const {
26 if (CallInst *CI = dyn_cast<CallInst>(I))
27 return CI->getCallingConv();
28 else
29 return cast<InvokeInst>(I)->getCallingConv();
30}
31void CallSite::setCallingConv(unsigned CC) {
32 if (CallInst *CI = dyn_cast<CallInst>(I))
33 CI->setCallingConv(CC);
34 else
35 cast<InvokeInst>(I)->setCallingConv(CC);
36}
37
38
Chris Lattner1c12a882006-06-21 16:53:47 +000039
40
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000041//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000042// TerminatorInst Class
43//===----------------------------------------------------------------------===//
44
Chris Lattner1c12a882006-06-21 16:53:47 +000045// Out of line virtual method, so the vtable, etc has a home.
46TerminatorInst::~TerminatorInst() {
47}
48
49// Out of line virtual method, so the vtable, etc has a home.
50UnaryInstruction::~UnaryInstruction() {
51}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000052
53
54//===----------------------------------------------------------------------===//
55// PHINode Class
56//===----------------------------------------------------------------------===//
57
58PHINode::PHINode(const PHINode &PN)
59 : Instruction(PN.getType(), Instruction::PHI,
60 new Use[PN.getNumOperands()], PN.getNumOperands()),
61 ReservedSpace(PN.getNumOperands()) {
62 Use *OL = OperandList;
63 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
64 OL[i].init(PN.getOperand(i), this);
65 OL[i+1].init(PN.getOperand(i+1), this);
66 }
67}
68
69PHINode::~PHINode() {
70 delete [] OperandList;
71}
72
73// removeIncomingValue - Remove an incoming value. This is useful if a
74// predecessor basic block is deleted.
75Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
76 unsigned NumOps = getNumOperands();
77 Use *OL = OperandList;
78 assert(Idx*2 < NumOps && "BB not in PHI node!");
79 Value *Removed = OL[Idx*2];
80
81 // Move everything after this operand down.
82 //
83 // FIXME: we could just swap with the end of the list, then erase. However,
84 // client might not expect this to happen. The code as it is thrashes the
85 // use/def lists, which is kinda lame.
86 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
87 OL[i-2] = OL[i];
88 OL[i-2+1] = OL[i+1];
89 }
90
91 // Nuke the last value.
92 OL[NumOps-2].set(0);
93 OL[NumOps-2+1].set(0);
94 NumOperands = NumOps-2;
95
96 // If the PHI node is dead, because it has zero entries, nuke it now.
97 if (NumOps == 2 && DeletePHIIfEmpty) {
98 // If anyone is using this PHI, make them use a dummy value instead...
99 replaceAllUsesWith(UndefValue::get(getType()));
100 eraseFromParent();
101 }
102 return Removed;
103}
104
105/// resizeOperands - resize operands - This adjusts the length of the operands
106/// list according to the following behavior:
107/// 1. If NumOps == 0, grow the operand list in response to a push_back style
108/// of operation. This grows the number of ops by 1.5 times.
109/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
110/// 3. If NumOps == NumOperands, trim the reserved space.
111///
112void PHINode::resizeOperands(unsigned NumOps) {
113 if (NumOps == 0) {
114 NumOps = (getNumOperands())*3/2;
115 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
116 } else if (NumOps*2 > NumOperands) {
117 // No resize needed.
118 if (ReservedSpace >= NumOps) return;
119 } else if (NumOps == NumOperands) {
120 if (ReservedSpace == NumOps) return;
121 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000123 }
124
125 ReservedSpace = NumOps;
126 Use *NewOps = new Use[NumOps];
127 Use *OldOps = OperandList;
128 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
129 NewOps[i].init(OldOps[i], this);
130 OldOps[i].set(0);
131 }
132 delete [] OldOps;
133 OperandList = NewOps;
134}
135
Nate Begemanb3923212005-08-04 23:24:19 +0000136/// hasConstantValue - If the specified PHI node always merges together the same
137/// value, return the value, otherwise return null.
138///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000139Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000140 // If the PHI node only has one incoming value, eliminate the PHI node...
141 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000142 if (getIncomingValue(0) != this) // not X = phi X
143 return getIncomingValue(0);
144 else
145 return UndefValue::get(getType()); // Self cycle is dead.
146
Nate Begemanb3923212005-08-04 23:24:19 +0000147 // Otherwise if all of the incoming values are the same for the PHI, replace
148 // the PHI node with the incoming value.
149 //
150 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000151 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000152 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000153 if (isa<UndefValue>(getIncomingValue(i)))
154 HasUndefInput = true;
155 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000156 if (InVal && getIncomingValue(i) != InVal)
157 return 0; // Not the same, bail out.
158 else
159 InVal = getIncomingValue(i);
160
161 // The only case that could cause InVal to be null is if we have a PHI node
162 // that only has entries for itself. In this case, there is no entry into the
163 // loop, so kill the PHI.
164 //
165 if (InVal == 0) InVal = UndefValue::get(getType());
166
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000167 // If we have a PHI node like phi(X, undef, X), where X is defined by some
168 // instruction, we cannot always return X as the result of the PHI node. Only
169 // do this if X is not an instruction (thus it must dominate the PHI block),
170 // or if the client is prepared to deal with this possibility.
171 if (HasUndefInput && !AllowNonDominatingInstruction)
172 if (Instruction *IV = dyn_cast<Instruction>(InVal))
173 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000174 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000175 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000176 return 0; // Cannot guarantee that InVal dominates this PHINode.
177
Nate Begemanb3923212005-08-04 23:24:19 +0000178 // All of the incoming values are the same, return the value now.
179 return InVal;
180}
181
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000182
183//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000184// CallInst Implementation
185//===----------------------------------------------------------------------===//
186
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187CallInst::~CallInst() {
188 delete [] OperandList;
189}
190
Chris Lattner054ba2c2007-02-13 00:58:44 +0000191void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000192 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000193 NumOperands = NumParams+1;
194 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000196
Misha Brukmanb1c93172005-04-21 23:48:37 +0000197 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000198 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000199 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000200
Chris Lattner054ba2c2007-02-13 00:58:44 +0000201 assert((NumParams == FTy->getNumParams() ||
202 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000203 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000204 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000205 assert((i >= FTy->getNumParams() ||
206 FTy->getParamType(i) == Params[i]->getType()) &&
207 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000208 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000209 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000210}
211
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000213 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000214 NumOperands = 3;
215 Use *OL = OperandList = new Use[3];
216 OL[0].init(Func, this);
217 OL[1].init(Actual1, this);
218 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000219
220 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000221 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000222 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000223
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000225 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000227 assert((0 >= FTy->getNumParams() ||
228 FTy->getParamType(0) == Actual1->getType()) &&
229 "Calling a function with a bad signature!");
230 assert((1 >= FTy->getNumParams() ||
231 FTy->getParamType(1) == Actual2->getType()) &&
232 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000233}
234
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000235void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000236 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000237 NumOperands = 2;
238 Use *OL = OperandList = new Use[2];
239 OL[0].init(Func, this);
240 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000241
242 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000244 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000245
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246 assert((FTy->getNumParams() == 1 ||
247 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000249 assert((0 == FTy->getNumParams() ||
250 FTy->getParamType(0) == Actual->getType()) &&
251 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252}
253
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000254void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000255 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000256 NumOperands = 1;
257 Use *OL = OperandList = new Use[1];
258 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000259
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000260 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000261 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000262 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000264 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265}
266
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000267CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000268 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
270 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000271 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000272 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000273 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000274}
275CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
276 const std::string &Name, Instruction *InsertBefore)
277: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
278 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000279 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000280 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000281 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000282}
283
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
285 const std::string &Name, Instruction *InsertBefore)
286 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
287 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000288 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000290 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291}
292
293CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
294 const std::string &Name, BasicBlock *InsertAtEnd)
295 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
296 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000297 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000299 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
302CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000303 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000306 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000308 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
311CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000315 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000317 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
320CallInst::CallInst(Value *Func, const std::string &Name,
321 Instruction *InsertBefore)
322 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
323 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000324 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000326 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327}
328
329CallInst::CallInst(Value *Func, const std::string &Name,
330 BasicBlock *InsertAtEnd)
331 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
332 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000333 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000335 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Misha Brukmanb1c93172005-04-21 23:48:37 +0000338CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
340 CI.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000341 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000342 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000343 Use *OL = OperandList;
344 Use *InOL = CI.OperandList;
345 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
346 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347}
348
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349
350//===----------------------------------------------------------------------===//
351// InvokeInst Implementation
352//===----------------------------------------------------------------------===//
353
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000354InvokeInst::~InvokeInst() {
355 delete [] OperandList;
356}
357
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000359 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000360 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000361 NumOperands = 3+NumArgs;
362 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000363 OL[0].init(Fn, this);
364 OL[1].init(IfNormal, this);
365 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000366 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000368 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000369
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000370 assert((NumArgs == FTy->getNumParams()) ||
371 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000373
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000374 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000375 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000376 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000377 "Invoking a function with a bad signature!");
378
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000379 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000380 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381}
382
383InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
384 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000385 Value* const *Args, unsigned NumArgs,
386 const std::string &Name, Instruction *InsertBefore)
387 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
388 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000389 Instruction::Invoke, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000390 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000391 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000392}
393
394InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
395 BasicBlock *IfException,
396 Value* const *Args, unsigned NumArgs,
397 const std::string &Name, BasicBlock *InsertAtEnd)
398 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
399 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000400 Instruction::Invoke, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000401 init(Fn, IfNormal, IfException, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000402 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000403}
404
Misha Brukmanb1c93172005-04-21 23:48:37 +0000405InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000406 : TerminatorInst(II.getType(), Instruction::Invoke,
407 new Use[II.getNumOperands()], II.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000408 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000409 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000410 Use *OL = OperandList, *InOL = II.OperandList;
411 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
412 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000413}
414
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000415BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
416 return getSuccessor(idx);
417}
418unsigned InvokeInst::getNumSuccessorsV() const {
419 return getNumSuccessors();
420}
421void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
422 return setSuccessor(idx, B);
423}
424
425
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426//===----------------------------------------------------------------------===//
427// ReturnInst Implementation
428//===----------------------------------------------------------------------===//
429
Chris Lattner2195fc42007-02-24 00:55:48 +0000430ReturnInst::ReturnInst(const ReturnInst &RI)
431 : TerminatorInst(Type::VoidTy, Instruction::Ret,
432 &RetVal, RI.getNumOperands()) {
433 if (RI.getNumOperands())
434 RetVal.init(RI.RetVal, this);
435}
436
437ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
438 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
439 init(retVal);
440}
441ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
442 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
443 init(retVal);
444}
445ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
446 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
447}
448
449
450
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000451void ReturnInst::init(Value *retVal) {
452 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000453 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000454 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455 NumOperands = 1;
456 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000457 }
458}
459
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000460unsigned ReturnInst::getNumSuccessorsV() const {
461 return getNumSuccessors();
462}
463
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000464// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
465// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000467 assert(0 && "ReturnInst has no successors!");
468}
469
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000470BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
471 assert(0 && "ReturnInst has no successors!");
472 abort();
473 return 0;
474}
475
476
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000477//===----------------------------------------------------------------------===//
478// UnwindInst Implementation
479//===----------------------------------------------------------------------===//
480
Chris Lattner2195fc42007-02-24 00:55:48 +0000481UnwindInst::UnwindInst(Instruction *InsertBefore)
482 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
483}
484UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
485 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
486}
487
488
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000489unsigned UnwindInst::getNumSuccessorsV() const {
490 return getNumSuccessors();
491}
492
493void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000494 assert(0 && "UnwindInst has no successors!");
495}
496
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000497BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
498 assert(0 && "UnwindInst has no successors!");
499 abort();
500 return 0;
501}
502
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000503//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000504// UnreachableInst Implementation
505//===----------------------------------------------------------------------===//
506
Chris Lattner2195fc42007-02-24 00:55:48 +0000507UnreachableInst::UnreachableInst(Instruction *InsertBefore)
508 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
509}
510UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
511 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
512}
513
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000514unsigned UnreachableInst::getNumSuccessorsV() const {
515 return getNumSuccessors();
516}
517
518void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
519 assert(0 && "UnwindInst has no successors!");
520}
521
522BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
523 assert(0 && "UnwindInst has no successors!");
524 abort();
525 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000526}
527
528//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000529// BranchInst Implementation
530//===----------------------------------------------------------------------===//
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532void BranchInst::AssertOK() {
533 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000534 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000535 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536}
537
Chris Lattner2195fc42007-02-24 00:55:48 +0000538BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
539 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
540 assert(IfTrue != 0 && "Branch destination may not be null!");
541 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
542}
543BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
544 Instruction *InsertBefore)
545: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
546 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
547 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
548 Ops[2].init(Cond, this);
549#ifndef NDEBUG
550 AssertOK();
551#endif
552}
553
554BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
555 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
556 assert(IfTrue != 0 && "Branch destination may not be null!");
557 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
558}
559
560BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
561 BasicBlock *InsertAtEnd)
562 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
563 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
564 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
565 Ops[2].init(Cond, this);
566#ifndef NDEBUG
567 AssertOK();
568#endif
569}
570
571
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000573 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574 OperandList[0].init(BI.getOperand(0), this);
575 if (BI.getNumOperands() != 1) {
576 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
577 OperandList[1].init(BI.getOperand(1), this);
578 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579 }
580}
581
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
583 return getSuccessor(idx);
584}
585unsigned BranchInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
587}
588void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
589 setSuccessor(idx, B);
590}
591
592
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593//===----------------------------------------------------------------------===//
594// AllocationInst Implementation
595//===----------------------------------------------------------------------===//
596
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000597static Value *getAISize(Value *Amt) {
598 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000599 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000600 else {
601 assert(!isa<BasicBlock>(Amt) &&
602 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000603 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000604 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000605 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000606 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
Misha Brukmanb1c93172005-04-21 23:48:37 +0000609AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000610 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000611 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000613 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000614 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000616 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000617}
618
Misha Brukmanb1c93172005-04-21 23:48:37 +0000619AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000620 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000623 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000624 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000626 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000627}
628
Chris Lattner1c12a882006-06-21 16:53:47 +0000629// Out of line virtual method, so the vtable, etc has a home.
630AllocationInst::~AllocationInst() {
631}
632
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000634 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
635 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000637}
638
639const Type *AllocationInst::getAllocatedType() const {
640 return getType()->getElementType();
641}
642
643AllocaInst::AllocaInst(const AllocaInst &AI)
644 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000645 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000646}
647
648MallocInst::MallocInst(const MallocInst &MI)
649 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000650 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000651}
652
653//===----------------------------------------------------------------------===//
654// FreeInst Implementation
655//===----------------------------------------------------------------------===//
656
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657void FreeInst::AssertOK() {
658 assert(isa<PointerType>(getOperand(0)->getType()) &&
659 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660}
661
662FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000663 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000665}
666
667FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000668 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000669 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000670}
671
672
673//===----------------------------------------------------------------------===//
674// LoadInst Implementation
675//===----------------------------------------------------------------------===//
676
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000678 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000679 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000680}
681
682LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000683 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000684 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000685 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000687 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688}
689
690LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000691 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000692 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000693 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000694 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000695 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000696}
697
698LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
699 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000701 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000702 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000704 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000705}
706
707LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
708 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000710 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000711 setVolatile(isVolatile);
712 AssertOK();
713 setName(Name);
714}
715
716
717
718LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000719 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
720 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000721 setVolatile(false);
722 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000723 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000724}
725
726LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000727 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
728 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000729 setVolatile(false);
730 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000731 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000732}
733
734LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
735 Instruction *InsertBef)
736: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000737 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000738 setVolatile(isVolatile);
739 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000740 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000741}
742
743LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
744 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000745 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
746 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000747 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000748 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000749 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750}
751
752
753//===----------------------------------------------------------------------===//
754// StoreInst Implementation
755//===----------------------------------------------------------------------===//
756
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757void StoreInst::AssertOK() {
758 assert(isa<PointerType>(getOperand(1)->getType()) &&
759 "Ptr must have pointer type!");
760 assert(getOperand(0)->getType() ==
761 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000762 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000763}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000764
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765
766StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000767 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000768 Ops[0].init(val, this);
769 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000770 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771 AssertOK();
772}
773
774StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000775 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000776 Ops[0].init(val, this);
777 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000778 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779 AssertOK();
780}
781
Misha Brukmanb1c93172005-04-21 23:48:37 +0000782StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000784 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785 Ops[0].init(val, this);
786 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000787 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788 AssertOK();
789}
790
Misha Brukmanb1c93172005-04-21 23:48:37 +0000791StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000793 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000794 Ops[0].init(val, this);
795 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000796 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798}
799
800//===----------------------------------------------------------------------===//
801// GetElementPtrInst Implementation
802//===----------------------------------------------------------------------===//
803
804// checkType - Simple wrapper function to give a better assertion failure
805// message on bad indexes for a gep instruction.
806//
807static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000808 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000809 return Ty;
810}
811
Chris Lattner79807c3d2007-01-31 19:47:18 +0000812void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
813 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000814 Use *OL = OperandList = new Use[NumOperands];
815 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816
Chris Lattner79807c3d2007-01-31 19:47:18 +0000817 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819}
820
821void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 NumOperands = 3;
823 Use *OL = OperandList = new Use[3];
824 OL[0].init(Ptr, this);
825 OL[1].init(Idx0, this);
826 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827}
828
Chris Lattner82981202005-05-03 05:43:30 +0000829void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
830 NumOperands = 2;
831 Use *OL = OperandList = new Use[2];
832 OL[0].init(Ptr, this);
833 OL[1].init(Idx, this);
834}
835
Chris Lattner79807c3d2007-01-31 19:47:18 +0000836
837GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
838 unsigned NumIdx,
839 const std::string &Name, Instruction *InBe)
840: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000841 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000842 GetElementPtr, 0, 0, InBe) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000843 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000844 setName(Name);
Chris Lattner79807c3d2007-01-31 19:47:18 +0000845}
846
847GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
848 unsigned NumIdx,
849 const std::string &Name, BasicBlock *IAE)
850: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000851 Idx, NumIdx, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 GetElementPtr, 0, 0, IAE) {
Chris Lattner79807c3d2007-01-31 19:47:18 +0000853 init(Ptr, Idx, NumIdx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855}
856
Chris Lattner82981202005-05-03 05:43:30 +0000857GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
858 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
860 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000861 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000863}
864
865GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
866 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000867 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
868 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000869 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000870 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000871}
872
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
874 const std::string &Name, Instruction *InBe)
875 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
876 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 GetElementPtr, 0, 0, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
882GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000883 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
885 Idx0, Idx1, true))),
Chris Lattner2195fc42007-02-24 00:55:48 +0000886 GetElementPtr, 0, 0, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887 init(Ptr, Idx0, Idx1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000888 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891GetElementPtrInst::~GetElementPtrInst() {
892 delete[] OperandList;
893}
894
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000895// getIndexedType - Returns the type of the element that would be loaded with
896// a load instruction with the specified parameters.
897//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000898// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899// pointer type.
900//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000901const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000902 Value* const *Idxs,
903 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904 bool AllowCompositeLeaf) {
905 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
906
907 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000908 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909 if (AllowCompositeLeaf ||
910 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
911 return cast<PointerType>(Ptr)->getElementType();
912 else
913 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000914
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000915 unsigned CurIdx = 0;
916 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000917 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
919 return 0; // Can't load a whole structure or array!?!?
920 }
921
Chris Lattner302116a2007-01-31 04:40:28 +0000922 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923 if (isa<PointerType>(CT) && CurIdx != 1)
924 return 0; // Can only index into pointer types at the first index!
925 if (!CT->indexValid(Index)) return 0;
926 Ptr = CT->getTypeAtIndex(Index);
927
928 // If the new type forwards to another type, then it is in the middle
929 // of being refined to another type (and hence, may have dropped all
930 // references to what it was using before). So, use the new forwarded
931 // type.
932 if (const Type * Ty = Ptr->getForwardedType()) {
933 Ptr = Ty;
934 }
935 }
Chris Lattner302116a2007-01-31 04:40:28 +0000936 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000937}
938
Misha Brukmanb1c93172005-04-21 23:48:37 +0000939const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000940 Value *Idx0, Value *Idx1,
941 bool AllowCompositeLeaf) {
942 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
943 if (!PTy) return 0; // Type isn't a pointer type!
944
945 // Check the pointer index.
946 if (!PTy->indexValid(Idx0)) return 0;
947
948 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
949 if (!CT || !CT->indexValid(Idx1)) return 0;
950
951 const Type *ElTy = CT->getTypeAtIndex(Idx1);
952 if (AllowCompositeLeaf || ElTy->isFirstClassType())
953 return ElTy;
954 return 0;
955}
956
Chris Lattner82981202005-05-03 05:43:30 +0000957const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
958 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
959 if (!PTy) return 0; // Type isn't a pointer type!
960
961 // Check the pointer index.
962 if (!PTy->indexValid(Idx)) return 0;
963
Chris Lattnerc2233332005-05-03 16:44:45 +0000964 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000965}
966
Chris Lattner45f15572007-04-14 00:12:57 +0000967
968/// hasAllZeroIndices - Return true if all of the indices of this GEP are
969/// zeros. If so, the result pointer and the first operand have the same
970/// value, just potentially different types.
971bool GetElementPtrInst::hasAllZeroIndices() const {
972 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
973 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
974 if (!CI->isZero()) return false;
975 } else {
976 return false;
977 }
978 }
979 return true;
980}
981
982
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000983//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000984// ExtractElementInst Implementation
985//===----------------------------------------------------------------------===//
986
987ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000988 const std::string &Name,
989 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000990 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000991 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000992 assert(isValidOperands(Val, Index) &&
993 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000994 Ops[0].init(Val, this);
995 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000996 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +0000997}
998
Chris Lattner65511ff2006-10-05 06:24:58 +0000999ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1000 const std::string &Name,
1001 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001002 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001003 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001004 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001005 assert(isValidOperands(Val, Index) &&
1006 "Invalid extractelement instruction operands!");
1007 Ops[0].init(Val, this);
1008 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001009 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001010}
1011
1012
Robert Bocchino23004482006-01-10 19:05:34 +00001013ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001014 const std::string &Name,
1015 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001016 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001017 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001018 assert(isValidOperands(Val, Index) &&
1019 "Invalid extractelement instruction operands!");
1020
Robert Bocchino23004482006-01-10 19:05:34 +00001021 Ops[0].init(Val, this);
1022 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001023 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001024}
1025
Chris Lattner65511ff2006-10-05 06:24:58 +00001026ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1027 const std::string &Name,
1028 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001029 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001030 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001031 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001032 assert(isValidOperands(Val, Index) &&
1033 "Invalid extractelement instruction operands!");
1034
1035 Ops[0].init(Val, this);
1036 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001037 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001038}
1039
1040
Chris Lattner54865b32006-04-08 04:05:48 +00001041bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001042 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001043 return false;
1044 return true;
1045}
1046
1047
Robert Bocchino23004482006-01-10 19:05:34 +00001048//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001049// InsertElementInst Implementation
1050//===----------------------------------------------------------------------===//
1051
Chris Lattner0875d942006-04-14 22:20:32 +00001052InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1053 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1054 Ops[0].init(IE.Ops[0], this);
1055 Ops[1].init(IE.Ops[1], this);
1056 Ops[2].init(IE.Ops[2], this);
1057}
Chris Lattner54865b32006-04-08 04:05:48 +00001058InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001059 const std::string &Name,
1060 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001061 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001062 assert(isValidOperands(Vec, Elt, Index) &&
1063 "Invalid insertelement instruction operands!");
1064 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001065 Ops[1].init(Elt, this);
1066 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001067 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001068}
1069
Chris Lattner65511ff2006-10-05 06:24:58 +00001070InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1071 const std::string &Name,
1072 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001073 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001074 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001075 assert(isValidOperands(Vec, Elt, Index) &&
1076 "Invalid insertelement instruction operands!");
1077 Ops[0].init(Vec, this);
1078 Ops[1].init(Elt, this);
1079 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001080 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001081}
1082
1083
Chris Lattner54865b32006-04-08 04:05:48 +00001084InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001085 const std::string &Name,
1086 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001087 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001088 assert(isValidOperands(Vec, Elt, Index) &&
1089 "Invalid insertelement instruction operands!");
1090
1091 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001092 Ops[1].init(Elt, this);
1093 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001094 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001095}
1096
Chris Lattner65511ff2006-10-05 06:24:58 +00001097InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1098 const std::string &Name,
1099 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001100: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001101 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001102 assert(isValidOperands(Vec, Elt, Index) &&
1103 "Invalid insertelement instruction operands!");
1104
1105 Ops[0].init(Vec, this);
1106 Ops[1].init(Elt, this);
1107 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001108 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001109}
1110
Chris Lattner54865b32006-04-08 04:05:48 +00001111bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1112 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001113 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001114 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001115
Reid Spencerd84d35b2007-02-15 02:26:10 +00001116 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Chris Lattner54865b32006-04-08 04:05:48 +00001117 return false;// Second operand of insertelement must be packed element type.
1118
Reid Spencer8d9336d2006-12-31 05:26:44 +00001119 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001120 return false; // Third operand of insertelement must be uint.
1121 return true;
1122}
1123
1124
Robert Bocchinoca27f032006-01-17 20:07:22 +00001125//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001126// ShuffleVectorInst Implementation
1127//===----------------------------------------------------------------------===//
1128
Chris Lattner0875d942006-04-14 22:20:32 +00001129ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1130 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1131 Ops[0].init(SV.Ops[0], this);
1132 Ops[1].init(SV.Ops[1], this);
1133 Ops[2].init(SV.Ops[2], this);
1134}
1135
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001136ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1137 const std::string &Name,
1138 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001139 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001140 assert(isValidOperands(V1, V2, Mask) &&
1141 "Invalid shuffle vector instruction operands!");
1142 Ops[0].init(V1, this);
1143 Ops[1].init(V2, this);
1144 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001145 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001146}
1147
1148ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1149 const std::string &Name,
1150 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001151 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001152 assert(isValidOperands(V1, V2, Mask) &&
1153 "Invalid shuffle vector instruction operands!");
1154
1155 Ops[0].init(V1, this);
1156 Ops[1].init(V2, this);
1157 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001158 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001159}
1160
1161bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1162 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001163 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001164 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001165 if (!isa<VectorType>(Mask->getType()) ||
1166 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1167 cast<VectorType>(Mask->getType())->getNumElements() !=
1168 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001169 return false;
1170 return true;
1171}
1172
1173
1174//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001175// BinaryOperator Class
1176//===----------------------------------------------------------------------===//
1177
Chris Lattner2195fc42007-02-24 00:55:48 +00001178BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1179 const Type *Ty, const std::string &Name,
1180 Instruction *InsertBefore)
1181 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1182 Ops[0].init(S1, this);
1183 Ops[1].init(S2, this);
1184 init(iType);
1185 setName(Name);
1186}
1187
1188BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1189 const Type *Ty, const std::string &Name,
1190 BasicBlock *InsertAtEnd)
1191 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1192 Ops[0].init(S1, this);
1193 Ops[1].init(S2, this);
1194 init(iType);
1195 setName(Name);
1196}
1197
1198
1199void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001200 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001201 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001202 assert(LHS->getType() == RHS->getType() &&
1203 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001204#ifndef NDEBUG
1205 switch (iType) {
1206 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001207 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001208 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001210 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001211 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001212 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001213 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001214 case UDiv:
1215 case SDiv:
1216 assert(getType() == LHS->getType() &&
1217 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001218 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1219 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001220 "Incorrect operand type (not integer) for S/UDIV");
1221 break;
1222 case FDiv:
1223 assert(getType() == LHS->getType() &&
1224 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001225 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1226 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001227 && "Incorrect operand type (not floating point) for FDIV");
1228 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001229 case URem:
1230 case SRem:
1231 assert(getType() == LHS->getType() &&
1232 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001233 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1234 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001235 "Incorrect operand type (not integer) for S/UREM");
1236 break;
1237 case FRem:
1238 assert(getType() == LHS->getType() &&
1239 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001240 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1241 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001242 && "Incorrect operand type (not floating point) for FREM");
1243 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001244 case Shl:
1245 case LShr:
1246 case AShr:
1247 assert(getType() == LHS->getType() &&
1248 "Shift operation should return same type as operands!");
1249 assert(getType()->isInteger() &&
1250 "Shift operation requires integer operands");
1251 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001252 case And: case Or:
1253 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001254 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001255 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001256 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001257 (isa<VectorType>(getType()) &&
1258 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001259 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001260 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001261 default:
1262 break;
1263 }
1264#endif
1265}
1266
1267BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001268 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001269 Instruction *InsertBefore) {
1270 assert(S1->getType() == S2->getType() &&
1271 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001272 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001273}
1274
1275BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001276 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001277 BasicBlock *InsertAtEnd) {
1278 BinaryOperator *Res = create(Op, S1, S2, Name);
1279 InsertAtEnd->getInstList().push_back(Res);
1280 return Res;
1281}
1282
1283BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1284 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001285 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1286 return new BinaryOperator(Instruction::Sub,
1287 zero, Op,
1288 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001289}
1290
1291BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1292 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001293 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1294 return new BinaryOperator(Instruction::Sub,
1295 zero, Op,
1296 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001297}
1298
1299BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1300 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001301 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001302 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001303 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001304 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001305 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001306 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001307 }
1308
1309 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310 Op->getType(), Name, InsertBefore);
1311}
1312
1313BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1314 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001315 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001316 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001317 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001318 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001319 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001320 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001321 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001322 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001323 }
1324
1325 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001326 Op->getType(), Name, InsertAtEnd);
1327}
1328
1329
1330// isConstantAllOnes - Helper function for several functions below
1331static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001332 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001333}
1334
1335bool BinaryOperator::isNeg(const Value *V) {
1336 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1337 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001338 return Bop->getOperand(0) ==
1339 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001340 return false;
1341}
1342
1343bool BinaryOperator::isNot(const Value *V) {
1344 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1345 return (Bop->getOpcode() == Instruction::Xor &&
1346 (isConstantAllOnes(Bop->getOperand(1)) ||
1347 isConstantAllOnes(Bop->getOperand(0))));
1348 return false;
1349}
1350
Chris Lattner2c7d1772005-04-24 07:28:37 +00001351Value *BinaryOperator::getNegArgument(Value *BinOp) {
1352 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1353 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001354}
1355
Chris Lattner2c7d1772005-04-24 07:28:37 +00001356const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1357 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001358}
1359
Chris Lattner2c7d1772005-04-24 07:28:37 +00001360Value *BinaryOperator::getNotArgument(Value *BinOp) {
1361 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1362 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1363 Value *Op0 = BO->getOperand(0);
1364 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001365 if (isConstantAllOnes(Op0)) return Op1;
1366
1367 assert(isConstantAllOnes(Op1));
1368 return Op0;
1369}
1370
Chris Lattner2c7d1772005-04-24 07:28:37 +00001371const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1372 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001373}
1374
1375
1376// swapOperands - Exchange the two operands to this instruction. This
1377// instruction is safe to use on any binary instruction and does not
1378// modify the semantics of the instruction. If the instruction is
1379// order dependent (SetLT f.e.) the opcode is changed.
1380//
1381bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001382 if (!isCommutative())
1383 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001384 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385 return false;
1386}
1387
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001388//===----------------------------------------------------------------------===//
1389// CastInst Class
1390//===----------------------------------------------------------------------===//
1391
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001392// Just determine if this cast only deals with integral->integral conversion.
1393bool CastInst::isIntegerCast() const {
1394 switch (getOpcode()) {
1395 default: return false;
1396 case Instruction::ZExt:
1397 case Instruction::SExt:
1398 case Instruction::Trunc:
1399 return true;
1400 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001401 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001402 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001403}
1404
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001405bool CastInst::isLosslessCast() const {
1406 // Only BitCast can be lossless, exit fast if we're not BitCast
1407 if (getOpcode() != Instruction::BitCast)
1408 return false;
1409
1410 // Identity cast is always lossless
1411 const Type* SrcTy = getOperand(0)->getType();
1412 const Type* DstTy = getType();
1413 if (SrcTy == DstTy)
1414 return true;
1415
Reid Spencer8d9336d2006-12-31 05:26:44 +00001416 // Pointer to pointer is always lossless.
1417 if (isa<PointerType>(SrcTy))
1418 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001419 return false; // Other types have no identity values
1420}
1421
1422/// This function determines if the CastInst does not require any bits to be
1423/// changed in order to effect the cast. Essentially, it identifies cases where
1424/// no code gen is necessary for the cast, hence the name no-op cast. For
1425/// example, the following are all no-op casts:
1426/// # bitcast uint %X, int
1427/// # bitcast uint* %x, sbyte*
1428/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1429/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1430/// @brief Determine if a cast is a no-op.
1431bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1432 switch (getOpcode()) {
1433 default:
1434 assert(!"Invalid CastOp");
1435 case Instruction::Trunc:
1436 case Instruction::ZExt:
1437 case Instruction::SExt:
1438 case Instruction::FPTrunc:
1439 case Instruction::FPExt:
1440 case Instruction::UIToFP:
1441 case Instruction::SIToFP:
1442 case Instruction::FPToUI:
1443 case Instruction::FPToSI:
1444 return false; // These always modify bits
1445 case Instruction::BitCast:
1446 return true; // BitCast never modifies bits.
1447 case Instruction::PtrToInt:
1448 return IntPtrTy->getPrimitiveSizeInBits() ==
1449 getType()->getPrimitiveSizeInBits();
1450 case Instruction::IntToPtr:
1451 return IntPtrTy->getPrimitiveSizeInBits() ==
1452 getOperand(0)->getType()->getPrimitiveSizeInBits();
1453 }
1454}
1455
1456/// This function determines if a pair of casts can be eliminated and what
1457/// opcode should be used in the elimination. This assumes that there are two
1458/// instructions like this:
1459/// * %F = firstOpcode SrcTy %x to MidTy
1460/// * %S = secondOpcode MidTy %F to DstTy
1461/// The function returns a resultOpcode so these two casts can be replaced with:
1462/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1463/// If no such cast is permited, the function returns 0.
1464unsigned CastInst::isEliminableCastPair(
1465 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1466 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1467{
1468 // Define the 144 possibilities for these two cast instructions. The values
1469 // in this matrix determine what to do in a given situation and select the
1470 // case in the switch below. The rows correspond to firstOp, the columns
1471 // correspond to secondOp. In looking at the table below, keep in mind
1472 // the following cast properties:
1473 //
1474 // Size Compare Source Destination
1475 // Operator Src ? Size Type Sign Type Sign
1476 // -------- ------------ ------------------- ---------------------
1477 // TRUNC > Integer Any Integral Any
1478 // ZEXT < Integral Unsigned Integer Any
1479 // SEXT < Integral Signed Integer Any
1480 // FPTOUI n/a FloatPt n/a Integral Unsigned
1481 // FPTOSI n/a FloatPt n/a Integral Signed
1482 // UITOFP n/a Integral Unsigned FloatPt n/a
1483 // SITOFP n/a Integral Signed FloatPt n/a
1484 // FPTRUNC > FloatPt n/a FloatPt n/a
1485 // FPEXT < FloatPt n/a FloatPt n/a
1486 // PTRTOINT n/a Pointer n/a Integral Unsigned
1487 // INTTOPTR n/a Integral Unsigned Pointer n/a
1488 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001489 //
1490 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1491 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1492 // into "fptoui double to ulong", but this loses information about the range
1493 // of the produced value (we no longer know the top-part is all zeros).
1494 // Further this conversion is often much more expensive for typical hardware,
1495 // and causes issues when building libgcc. We disallow fptosi+sext for the
1496 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001497 const unsigned numCastOps =
1498 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1499 static const uint8_t CastResults[numCastOps][numCastOps] = {
1500 // T F F U S F F P I B -+
1501 // R Z S P P I I T P 2 N T |
1502 // U E E 2 2 2 2 R E I T C +- secondOp
1503 // N X X U S F F N X N 2 V |
1504 // C T T I I P P C T T P T -+
1505 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1506 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1507 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001508 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1509 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001510 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1511 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1512 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1513 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1514 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1515 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1516 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1517 };
1518
1519 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1520 [secondOp-Instruction::CastOpsBegin];
1521 switch (ElimCase) {
1522 case 0:
1523 // categorically disallowed
1524 return 0;
1525 case 1:
1526 // allowed, use first cast's opcode
1527 return firstOp;
1528 case 2:
1529 // allowed, use second cast's opcode
1530 return secondOp;
1531 case 3:
1532 // no-op cast in second op implies firstOp as long as the DestTy
1533 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001534 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001535 return firstOp;
1536 return 0;
1537 case 4:
1538 // no-op cast in second op implies firstOp as long as the DestTy
1539 // is floating point
1540 if (DstTy->isFloatingPoint())
1541 return firstOp;
1542 return 0;
1543 case 5:
1544 // no-op cast in first op implies secondOp as long as the SrcTy
1545 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001546 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001547 return secondOp;
1548 return 0;
1549 case 6:
1550 // no-op cast in first op implies secondOp as long as the SrcTy
1551 // is a floating point
1552 if (SrcTy->isFloatingPoint())
1553 return secondOp;
1554 return 0;
1555 case 7: {
1556 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1557 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1558 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1559 if (MidSize >= PtrSize)
1560 return Instruction::BitCast;
1561 return 0;
1562 }
1563 case 8: {
1564 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1565 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1566 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1567 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1568 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1569 if (SrcSize == DstSize)
1570 return Instruction::BitCast;
1571 else if (SrcSize < DstSize)
1572 return firstOp;
1573 return secondOp;
1574 }
1575 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1576 return Instruction::ZExt;
1577 case 10:
1578 // fpext followed by ftrunc is allowed if the bit size returned to is
1579 // the same as the original, in which case its just a bitcast
1580 if (SrcTy == DstTy)
1581 return Instruction::BitCast;
1582 return 0; // If the types are not the same we can't eliminate it.
1583 case 11:
1584 // bitcast followed by ptrtoint is allowed as long as the bitcast
1585 // is a pointer to pointer cast.
1586 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1587 return secondOp;
1588 return 0;
1589 case 12:
1590 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1591 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1592 return firstOp;
1593 return 0;
1594 case 13: {
1595 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1596 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1597 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1598 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1599 if (SrcSize <= PtrSize && SrcSize == DstSize)
1600 return Instruction::BitCast;
1601 return 0;
1602 }
1603 case 99:
1604 // cast combination can't happen (error in input). This is for all cases
1605 // where the MidTy is not the same for the two cast instructions.
1606 assert(!"Invalid Cast Combination");
1607 return 0;
1608 default:
1609 assert(!"Error in CastResults table!!!");
1610 return 0;
1611 }
1612 return 0;
1613}
1614
1615CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1616 const std::string &Name, Instruction *InsertBefore) {
1617 // Construct and return the appropriate CastInst subclass
1618 switch (op) {
1619 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1620 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1621 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1622 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1623 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1624 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1625 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1626 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1627 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1628 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1629 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1630 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1631 default:
1632 assert(!"Invalid opcode provided");
1633 }
1634 return 0;
1635}
1636
1637CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1638 const std::string &Name, BasicBlock *InsertAtEnd) {
1639 // Construct and return the appropriate CastInst subclass
1640 switch (op) {
1641 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1642 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1643 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1644 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1645 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1646 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1647 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1648 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1649 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1650 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1651 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1652 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1653 default:
1654 assert(!"Invalid opcode provided");
1655 }
1656 return 0;
1657}
1658
Reid Spencer5c140882006-12-04 20:17:56 +00001659CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1660 const std::string &Name,
1661 Instruction *InsertBefore) {
1662 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1663 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1664 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1665}
1666
1667CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1668 const std::string &Name,
1669 BasicBlock *InsertAtEnd) {
1670 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1671 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1672 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1673}
1674
1675CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1676 const std::string &Name,
1677 Instruction *InsertBefore) {
1678 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1679 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1680 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1681}
1682
1683CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1684 const std::string &Name,
1685 BasicBlock *InsertAtEnd) {
1686 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1687 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1688 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1689}
1690
1691CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1692 const std::string &Name,
1693 Instruction *InsertBefore) {
1694 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1695 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1696 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1697}
1698
1699CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1700 const std::string &Name,
1701 BasicBlock *InsertAtEnd) {
1702 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1703 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1704 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1705}
1706
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001707CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1708 const std::string &Name,
1709 BasicBlock *InsertAtEnd) {
1710 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001711 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001712 "Invalid cast");
1713
Chris Lattner03c49532007-01-15 02:27:26 +00001714 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001715 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1716 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1717}
1718
1719/// @brief Create a BitCast or a PtrToInt cast instruction
1720CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1721 const std::string &Name,
1722 Instruction *InsertBefore) {
1723 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001724 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001725 "Invalid cast");
1726
Chris Lattner03c49532007-01-15 02:27:26 +00001727 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001728 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1729 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1730}
1731
Reid Spencer7e933472006-12-12 00:49:44 +00001732CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1733 bool isSigned, const std::string &Name,
1734 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001735 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001736 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1737 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1738 Instruction::CastOps opcode =
1739 (SrcBits == DstBits ? Instruction::BitCast :
1740 (SrcBits > DstBits ? Instruction::Trunc :
1741 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1742 return create(opcode, C, Ty, Name, InsertBefore);
1743}
1744
1745CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1746 bool isSigned, const std::string &Name,
1747 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001748 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001749 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1750 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1751 Instruction::CastOps opcode =
1752 (SrcBits == DstBits ? Instruction::BitCast :
1753 (SrcBits > DstBits ? Instruction::Trunc :
1754 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1755 return create(opcode, C, Ty, Name, InsertAtEnd);
1756}
1757
1758CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1759 const std::string &Name,
1760 Instruction *InsertBefore) {
1761 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1762 "Invalid cast");
1763 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1764 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1765 Instruction::CastOps opcode =
1766 (SrcBits == DstBits ? Instruction::BitCast :
1767 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1768 return create(opcode, C, Ty, Name, InsertBefore);
1769}
1770
1771CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1772 const std::string &Name,
1773 BasicBlock *InsertAtEnd) {
1774 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1775 "Invalid cast");
1776 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1777 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1778 Instruction::CastOps opcode =
1779 (SrcBits == DstBits ? Instruction::BitCast :
1780 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1781 return create(opcode, C, Ty, Name, InsertAtEnd);
1782}
1783
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001784// Provide a way to get a "cast" where the cast opcode is inferred from the
1785// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001786// logic in the castIsValid function below. This axiom should hold:
1787// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1788// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789// casting opcode for the arguments passed to it.
1790Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001791CastInst::getCastOpcode(
1792 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 // Get the bit sizes, we'll need these
1794 const Type *SrcTy = Src->getType();
1795 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1796 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1797
1798 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001799 if (DestTy->isInteger()) { // Casting to integral
1800 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001801 if (DestBits < SrcBits)
1802 return Trunc; // int -> smaller int
1803 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001804 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001805 return SExt; // signed -> SEXT
1806 else
1807 return ZExt; // unsigned -> ZEXT
1808 } else {
1809 return BitCast; // Same size, No-op cast
1810 }
1811 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001812 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813 return FPToSI; // FP -> sint
1814 else
1815 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001816 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817 assert(DestBits == PTy->getBitWidth() &&
1818 "Casting packed to integer of different width");
1819 return BitCast; // Same size, no-op cast
1820 } else {
1821 assert(isa<PointerType>(SrcTy) &&
1822 "Casting from a value that is not first-class type");
1823 return PtrToInt; // ptr -> int
1824 }
1825 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001826 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001827 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001828 return SIToFP; // sint -> FP
1829 else
1830 return UIToFP; // uint -> FP
1831 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1832 if (DestBits < SrcBits) {
1833 return FPTrunc; // FP -> smaller FP
1834 } else if (DestBits > SrcBits) {
1835 return FPExt; // FP -> larger FP
1836 } else {
1837 return BitCast; // same size, no-op cast
1838 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001839 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840 assert(DestBits == PTy->getBitWidth() &&
1841 "Casting packed to floating point of different width");
1842 return BitCast; // same size, no-op cast
1843 } else {
1844 assert(0 && "Casting pointer or non-first class to float");
1845 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001846 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1847 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001848 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1849 "Casting packed to packed of different widths");
1850 return BitCast; // packed -> packed
1851 } else if (DestPTy->getBitWidth() == SrcBits) {
1852 return BitCast; // float/int -> packed
1853 } else {
1854 assert(!"Illegal cast to packed (wrong type or size)");
1855 }
1856 } else if (isa<PointerType>(DestTy)) {
1857 if (isa<PointerType>(SrcTy)) {
1858 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001859 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001860 return IntToPtr; // int -> ptr
1861 } else {
1862 assert(!"Casting pointer to other than pointer or int");
1863 }
1864 } else {
1865 assert(!"Casting to type that is not first-class");
1866 }
1867
1868 // If we fall through to here we probably hit an assertion cast above
1869 // and assertions are not turned on. Anything we return is an error, so
1870 // BitCast is as good a choice as any.
1871 return BitCast;
1872}
1873
1874//===----------------------------------------------------------------------===//
1875// CastInst SubClass Constructors
1876//===----------------------------------------------------------------------===//
1877
1878/// Check that the construction parameters for a CastInst are correct. This
1879/// could be broken out into the separate constructors but it is useful to have
1880/// it in one place and to eliminate the redundant code for getting the sizes
1881/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001882bool
1883CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001884
1885 // Check for type sanity on the arguments
1886 const Type *SrcTy = S->getType();
1887 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1888 return false;
1889
1890 // Get the size of the types in bits, we'll need this later
1891 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1892 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1893
1894 // Switch on the opcode provided
1895 switch (op) {
1896 default: return false; // This is an input error
1897 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001898 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001900 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001902 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 case Instruction::FPTrunc:
1904 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1905 SrcBitSize > DstBitSize;
1906 case Instruction::FPExt:
1907 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1908 SrcBitSize < DstBitSize;
1909 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001910 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001911 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001912 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001913 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001914 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001915 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001916 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001917 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001918 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001920 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001921 case Instruction::BitCast:
1922 // BitCast implies a no-op cast of type only. No bits change.
1923 // However, you can't cast pointers to anything but pointers.
1924 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1925 return false;
1926
1927 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1928 // these cases, the cast is okay if the source and destination bit widths
1929 // are identical.
1930 return SrcBitSize == DstBitSize;
1931 }
1932}
1933
1934TruncInst::TruncInst(
1935 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1936) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001937 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938}
1939
1940TruncInst::TruncInst(
1941 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1942) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001943 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001944}
1945
1946ZExtInst::ZExtInst(
1947 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1948) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001949 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950}
1951
1952ZExtInst::ZExtInst(
1953 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1954) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001955 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956}
1957SExtInst::SExtInst(
1958 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1959) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961}
1962
Jeff Cohencc08c832006-12-02 02:22:01 +00001963SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001964 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1965) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001966 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967}
1968
1969FPTruncInst::FPTruncInst(
1970 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1971) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001972 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973}
1974
1975FPTruncInst::FPTruncInst(
1976 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1977) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001978 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001979}
1980
1981FPExtInst::FPExtInst(
1982 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1983) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001984 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001985}
1986
1987FPExtInst::FPExtInst(
1988 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1989) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001990 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991}
1992
1993UIToFPInst::UIToFPInst(
1994 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1995) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001996 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997}
1998
1999UIToFPInst::UIToFPInst(
2000 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2001) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002002 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003}
2004
2005SIToFPInst::SIToFPInst(
2006 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2007) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002008 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009}
2010
2011SIToFPInst::SIToFPInst(
2012 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2013) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002014 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015}
2016
2017FPToUIInst::FPToUIInst(
2018 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2019) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002020 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021}
2022
2023FPToUIInst::FPToUIInst(
2024 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2025) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002026 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002027}
2028
2029FPToSIInst::FPToSIInst(
2030 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2031) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002032 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033}
2034
2035FPToSIInst::FPToSIInst(
2036 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2037) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002038 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039}
2040
2041PtrToIntInst::PtrToIntInst(
2042 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2043) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002044 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045}
2046
2047PtrToIntInst::PtrToIntInst(
2048 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2049) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002050 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002051}
2052
2053IntToPtrInst::IntToPtrInst(
2054 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2055) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002056 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002057}
2058
2059IntToPtrInst::IntToPtrInst(
2060 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2061) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002062 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063}
2064
2065BitCastInst::BitCastInst(
2066 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2067) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002068 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002069}
2070
2071BitCastInst::BitCastInst(
2072 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2073) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002074 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002075}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002076
2077//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002078// CmpInst Classes
2079//===----------------------------------------------------------------------===//
2080
2081CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2082 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002083 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002084 Ops[0].init(LHS, this);
2085 Ops[1].init(RHS, this);
2086 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002087 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002088 if (op == Instruction::ICmp) {
2089 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2090 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2091 "Invalid ICmp predicate value");
2092 const Type* Op0Ty = getOperand(0)->getType();
2093 const Type* Op1Ty = getOperand(1)->getType();
2094 assert(Op0Ty == Op1Ty &&
2095 "Both operands to ICmp instruction are not of the same type!");
2096 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002097 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002098 "Invalid operand types for ICmp instruction");
2099 return;
2100 }
2101 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2102 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2103 "Invalid FCmp predicate value");
2104 const Type* Op0Ty = getOperand(0)->getType();
2105 const Type* Op1Ty = getOperand(1)->getType();
2106 assert(Op0Ty == Op1Ty &&
2107 "Both operands to FCmp 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->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002110 "Invalid operand types for FCmp instruction");
2111}
2112
2113CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2114 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002115 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002116 Ops[0].init(LHS, this);
2117 Ops[1].init(RHS, this);
2118 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002119 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002120 if (op == Instruction::ICmp) {
2121 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2122 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2123 "Invalid ICmp predicate value");
2124
2125 const Type* Op0Ty = getOperand(0)->getType();
2126 const Type* Op1Ty = getOperand(1)->getType();
2127 assert(Op0Ty == Op1Ty &&
2128 "Both operands to ICmp instruction are not of the same type!");
2129 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002130 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002131 "Invalid operand types for ICmp instruction");
2132 return;
2133 }
2134 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2135 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2136 "Invalid FCmp predicate value");
2137 const Type* Op0Ty = getOperand(0)->getType();
2138 const Type* Op1Ty = getOperand(1)->getType();
2139 assert(Op0Ty == Op1Ty &&
2140 "Both operands to FCmp instruction are not of the same type!");
2141 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002142 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002143 "Invalid operand types for FCmp instruction");
2144}
2145
2146CmpInst *
2147CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2148 const std::string &Name, Instruction *InsertBefore) {
2149 if (Op == Instruction::ICmp) {
2150 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2151 InsertBefore);
2152 }
2153 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2154 InsertBefore);
2155}
2156
2157CmpInst *
2158CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2159 const std::string &Name, BasicBlock *InsertAtEnd) {
2160 if (Op == Instruction::ICmp) {
2161 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2162 InsertAtEnd);
2163 }
2164 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2165 InsertAtEnd);
2166}
2167
2168void CmpInst::swapOperands() {
2169 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2170 IC->swapOperands();
2171 else
2172 cast<FCmpInst>(this)->swapOperands();
2173}
2174
2175bool CmpInst::isCommutative() {
2176 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2177 return IC->isCommutative();
2178 return cast<FCmpInst>(this)->isCommutative();
2179}
2180
2181bool CmpInst::isEquality() {
2182 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2183 return IC->isEquality();
2184 return cast<FCmpInst>(this)->isEquality();
2185}
2186
2187
2188ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2189 switch (pred) {
2190 default:
2191 assert(!"Unknown icmp predicate!");
2192 case ICMP_EQ: return ICMP_NE;
2193 case ICMP_NE: return ICMP_EQ;
2194 case ICMP_UGT: return ICMP_ULE;
2195 case ICMP_ULT: return ICMP_UGE;
2196 case ICMP_UGE: return ICMP_ULT;
2197 case ICMP_ULE: return ICMP_UGT;
2198 case ICMP_SGT: return ICMP_SLE;
2199 case ICMP_SLT: return ICMP_SGE;
2200 case ICMP_SGE: return ICMP_SLT;
2201 case ICMP_SLE: return ICMP_SGT;
2202 }
2203}
2204
2205ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2206 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002207 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002208 case ICMP_EQ: case ICMP_NE:
2209 return pred;
2210 case ICMP_SGT: return ICMP_SLT;
2211 case ICMP_SLT: return ICMP_SGT;
2212 case ICMP_SGE: return ICMP_SLE;
2213 case ICMP_SLE: return ICMP_SGE;
2214 case ICMP_UGT: return ICMP_ULT;
2215 case ICMP_ULT: return ICMP_UGT;
2216 case ICMP_UGE: return ICMP_ULE;
2217 case ICMP_ULE: return ICMP_UGE;
2218 }
2219}
2220
Reid Spencer266e42b2006-12-23 06:05:41 +00002221ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2222 switch (pred) {
2223 default: assert(! "Unknown icmp predicate!");
2224 case ICMP_EQ: case ICMP_NE:
2225 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2226 return pred;
2227 case ICMP_UGT: return ICMP_SGT;
2228 case ICMP_ULT: return ICMP_SLT;
2229 case ICMP_UGE: return ICMP_SGE;
2230 case ICMP_ULE: return ICMP_SLE;
2231 }
2232}
2233
2234bool ICmpInst::isSignedPredicate(Predicate pred) {
2235 switch (pred) {
2236 default: assert(! "Unknown icmp predicate!");
2237 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2238 return true;
2239 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2240 case ICMP_UGE: case ICMP_ULE:
2241 return false;
2242 }
2243}
2244
Reid Spencer0286bc12007-02-28 22:00:54 +00002245/// Initialize a set of values that all satisfy the condition with C.
2246///
2247ConstantRange
2248ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2249 APInt Lower(C);
2250 APInt Upper(C);
2251 uint32_t BitWidth = C.getBitWidth();
2252 switch (pred) {
2253 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2254 case ICmpInst::ICMP_EQ: Upper++; break;
2255 case ICmpInst::ICMP_NE: Lower++; break;
2256 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2257 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2258 case ICmpInst::ICMP_UGT:
2259 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2260 break;
2261 case ICmpInst::ICMP_SGT:
2262 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2263 break;
2264 case ICmpInst::ICMP_ULE:
2265 Lower = APInt::getMinValue(BitWidth); Upper++;
2266 break;
2267 case ICmpInst::ICMP_SLE:
2268 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2269 break;
2270 case ICmpInst::ICMP_UGE:
2271 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2272 break;
2273 case ICmpInst::ICMP_SGE:
2274 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2275 break;
2276 }
2277 return ConstantRange(Lower, Upper);
2278}
2279
Reid Spencerd9436b62006-11-20 01:22:35 +00002280FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2281 switch (pred) {
2282 default:
2283 assert(!"Unknown icmp predicate!");
2284 case FCMP_OEQ: return FCMP_UNE;
2285 case FCMP_ONE: return FCMP_UEQ;
2286 case FCMP_OGT: return FCMP_ULE;
2287 case FCMP_OLT: return FCMP_UGE;
2288 case FCMP_OGE: return FCMP_ULT;
2289 case FCMP_OLE: return FCMP_UGT;
2290 case FCMP_UEQ: return FCMP_ONE;
2291 case FCMP_UNE: return FCMP_OEQ;
2292 case FCMP_UGT: return FCMP_OLE;
2293 case FCMP_ULT: return FCMP_OGE;
2294 case FCMP_UGE: return FCMP_OLT;
2295 case FCMP_ULE: return FCMP_OGT;
2296 case FCMP_ORD: return FCMP_UNO;
2297 case FCMP_UNO: return FCMP_ORD;
2298 case FCMP_TRUE: return FCMP_FALSE;
2299 case FCMP_FALSE: return FCMP_TRUE;
2300 }
2301}
2302
2303FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2304 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002305 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002306 case FCMP_FALSE: case FCMP_TRUE:
2307 case FCMP_OEQ: case FCMP_ONE:
2308 case FCMP_UEQ: case FCMP_UNE:
2309 case FCMP_ORD: case FCMP_UNO:
2310 return pred;
2311 case FCMP_OGT: return FCMP_OLT;
2312 case FCMP_OLT: return FCMP_OGT;
2313 case FCMP_OGE: return FCMP_OLE;
2314 case FCMP_OLE: return FCMP_OGE;
2315 case FCMP_UGT: return FCMP_ULT;
2316 case FCMP_ULT: return FCMP_UGT;
2317 case FCMP_UGE: return FCMP_ULE;
2318 case FCMP_ULE: return FCMP_UGE;
2319 }
2320}
2321
Reid Spencer266e42b2006-12-23 06:05:41 +00002322bool CmpInst::isUnsigned(unsigned short predicate) {
2323 switch (predicate) {
2324 default: return false;
2325 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2326 case ICmpInst::ICMP_UGE: return true;
2327 }
2328}
2329
2330bool CmpInst::isSigned(unsigned short predicate){
2331 switch (predicate) {
2332 default: return false;
2333 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2334 case ICmpInst::ICMP_SGE: return true;
2335 }
2336}
2337
2338bool CmpInst::isOrdered(unsigned short predicate) {
2339 switch (predicate) {
2340 default: return false;
2341 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2342 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2343 case FCmpInst::FCMP_ORD: return true;
2344 }
2345}
2346
2347bool CmpInst::isUnordered(unsigned short predicate) {
2348 switch (predicate) {
2349 default: return false;
2350 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2351 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2352 case FCmpInst::FCMP_UNO: return true;
2353 }
2354}
2355
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002356//===----------------------------------------------------------------------===//
2357// SwitchInst Implementation
2358//===----------------------------------------------------------------------===//
2359
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002360void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002361 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002362 ReservedSpace = 2+NumCases*2;
2363 NumOperands = 2;
2364 OperandList = new Use[ReservedSpace];
2365
2366 OperandList[0].init(Value, this);
2367 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002368}
2369
Chris Lattner2195fc42007-02-24 00:55:48 +00002370/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2371/// switch on and a default destination. The number of additional cases can
2372/// be specified here to make memory allocation more efficient. This
2373/// constructor can also autoinsert before another instruction.
2374SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2375 Instruction *InsertBefore)
2376 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2377 init(Value, Default, NumCases);
2378}
2379
2380/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2381/// switch on and a default destination. The number of additional cases can
2382/// be specified here to make memory allocation more efficient. This
2383/// constructor also autoinserts at the end of the specified BasicBlock.
2384SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2385 BasicBlock *InsertAtEnd)
2386 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2387 init(Value, Default, NumCases);
2388}
2389
Misha Brukmanb1c93172005-04-21 23:48:37 +00002390SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002391 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2392 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002393 Use *OL = OperandList, *InOL = SI.OperandList;
2394 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2395 OL[i].init(InOL[i], this);
2396 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002397 }
2398}
2399
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002400SwitchInst::~SwitchInst() {
2401 delete [] OperandList;
2402}
2403
2404
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002405/// addCase - Add an entry to the switch instruction...
2406///
Chris Lattner47ac1872005-02-24 05:32:09 +00002407void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002408 unsigned OpNo = NumOperands;
2409 if (OpNo+2 > ReservedSpace)
2410 resizeOperands(0); // Get more space!
2411 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002412 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002413 NumOperands = OpNo+2;
2414 OperandList[OpNo].init(OnVal, this);
2415 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002416}
2417
2418/// removeCase - This method removes the specified successor from the switch
2419/// instruction. Note that this cannot be used to remove the default
2420/// destination (successor #0).
2421///
2422void SwitchInst::removeCase(unsigned idx) {
2423 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002424 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2425
2426 unsigned NumOps = getNumOperands();
2427 Use *OL = OperandList;
2428
2429 // Move everything after this operand down.
2430 //
2431 // FIXME: we could just swap with the end of the list, then erase. However,
2432 // client might not expect this to happen. The code as it is thrashes the
2433 // use/def lists, which is kinda lame.
2434 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2435 OL[i-2] = OL[i];
2436 OL[i-2+1] = OL[i+1];
2437 }
2438
2439 // Nuke the last value.
2440 OL[NumOps-2].set(0);
2441 OL[NumOps-2+1].set(0);
2442 NumOperands = NumOps-2;
2443}
2444
2445/// resizeOperands - resize operands - This adjusts the length of the operands
2446/// list according to the following behavior:
2447/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2448/// of operation. This grows the number of ops by 1.5 times.
2449/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2450/// 3. If NumOps == NumOperands, trim the reserved space.
2451///
2452void SwitchInst::resizeOperands(unsigned NumOps) {
2453 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002454 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002455 } else if (NumOps*2 > NumOperands) {
2456 // No resize needed.
2457 if (ReservedSpace >= NumOps) return;
2458 } else if (NumOps == NumOperands) {
2459 if (ReservedSpace == NumOps) return;
2460 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002461 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002462 }
2463
2464 ReservedSpace = NumOps;
2465 Use *NewOps = new Use[NumOps];
2466 Use *OldOps = OperandList;
2467 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2468 NewOps[i].init(OldOps[i], this);
2469 OldOps[i].set(0);
2470 }
2471 delete [] OldOps;
2472 OperandList = NewOps;
2473}
2474
2475
2476BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2477 return getSuccessor(idx);
2478}
2479unsigned SwitchInst::getNumSuccessorsV() const {
2480 return getNumSuccessors();
2481}
2482void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2483 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002484}
Chris Lattnerf22be932004-10-15 23:52:53 +00002485
2486
2487// Define these methods here so vtables don't get emitted into every translation
2488// unit that uses these classes.
2489
2490GetElementPtrInst *GetElementPtrInst::clone() const {
2491 return new GetElementPtrInst(*this);
2492}
2493
2494BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002495 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002496}
2497
Reid Spencerd9436b62006-11-20 01:22:35 +00002498CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002499 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002500}
2501
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2503AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2504FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2505LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2506StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2507CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2508CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2509CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2510CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2511CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2512CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2513CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2514CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2515CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2516CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2517CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2518CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2519CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2521VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2522
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002523ExtractElementInst *ExtractElementInst::clone() const {
2524 return new ExtractElementInst(*this);
2525}
2526InsertElementInst *InsertElementInst::clone() const {
2527 return new InsertElementInst(*this);
2528}
2529ShuffleVectorInst *ShuffleVectorInst::clone() const {
2530 return new ShuffleVectorInst(*this);
2531}
Chris Lattnerf22be932004-10-15 23:52:53 +00002532PHINode *PHINode::clone() const { return new PHINode(*this); }
2533ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2534BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2535SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2536InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2537UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002538UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}